Python Files All In One
Python Files All In One
open, read, write, append, binary, close
mode
You can specify the mode
used to open a file by applying a second argument to the open function.
Sending "r" means open in read
mode, which is the default.
Sending "w" means write
mode, for rewriting the contents of a file.
Sending "a" means append
mode, for adding new content to the end of the file.
Adding "b" to a mode opens it in binary
mode, which is used for non-text
files (such as image
and sound
files).
You can combine
modes, for example, wb
from the code above opens the file in binary write
mode.
t
=> text
mode
# write mode
open("filename.txt", "w")
# read mode
open("filename.txt", "r")
# 等价于
# open("filename.txt")
# append mode
open("filename.txt", "a")
# binary write mode
open("filename.txt", "wb")
# binary read mode
open("filename.txt", "rb")
# binary append mode
open("filename.txt", "ab")
"""
$ py3 ./files-open-mode.py
"""
open
# You can use Python to read and write the contents of files.
# The argument of the open function is the path to the file.
# If the file is in the current working directory of the program, you can specify only its name.
file1 = open("filename.txt")
file2 = open("./filename.txt")
print("file1 =", file1)
print("file2 =", file2)
"""
$ py3 ./files-open.py
file1 = <_io.TextIOWrapper name='filename.txt' mode='r' encoding='UTF-8'>
file2 = <_io.TextIOWrapper name='./filename.txt' mode='r' encoding='UTF-8'>
"""
read
# file = open("filename.txt", "r")
file = open("filename.txt")
content = file.read()
print("content =\n", content)
file.close()
"""
$ py3 ./files-reading.py
content =
# CSV
file1, 2012
file2, 2016
file3, 2020
file4, 2024
"""
# To read only a certain amount of a file, you can provide the number of `bytes` to read as an argument to the read function.
# Each `ASCII` character is 1 byte:
# file = open("filename.txt", "r")
file = open("filename.txt")
content1 = file.read(10)
content2 = file.read(20)
content3 = file.read(30)
# 剩余的
content = file.read()
print("content 1 =\n", content1)
print("content 2 =\n", content2)
print("content 3 =\n", content3)
print("🚀 content all =\n", content)
file.close()
"""
$ py3 ./files-reading-bytes.py
content 1 =
# CSV file
content 2 =
file1, 2012
file2,
content 3 =
2016
file3, 2020
file4, 2024
#
🚀 content all =
begin
#############################
10 10 10 10 10 10 10 10 10 10
20 20 20 20 20 20 20 20 20 20
30 30 30 30 30 30 30 30 30 30
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
# ends
"""
print("\n ❓❓❓❓❓❓❓❓❓❓❓")
# bug ❌ read order ❌
file = open("filename.txt")
content = file.read()
content1 = file.read(10)
content2 = file.read(20)
content3 = file.read(30)
print("content all =\n", content)
print("content 1 =\n", content1)
print("content 2 =\n", content2)
print("content 3 =\n", content3)
# print("content all =\n", content)
file.close()
"""
$ py3 ./files-reading-bytes.py
❓❓❓❓❓❓❓❓❓❓❓
content all =
# CSV file
file1, 2012
file2, 2016
file3, 2020
file4, 2024
# begin
#############################
10 10 10 10 10 10 10 10 10 10
20 20 20 20 20 20 20 20 20 20
30 30 30 30 30 30 30 30 30 30
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
# ends
content 1 =
content 2 =
content 3 =
"""
# readlines
file = open("../files/books.txt")
# index range
for line in file.readlines():
print("✅ line =", line)
print(file.readlines())
file.close()
print("\n")
# If you do not need the list for each line, you can simply `iterate over` the file variable:
file2 = open("../files/words.txt")
for line in file2:
print("✅✅ line =", line)
print(file2)
file2.close()
"""
$ py3 ./files-reading-readlines.py
✅ line = Harry Potter
✅ line = The Hunger Games
✅ line = Pride and Prejudice
✅ line = Gone with the Wind
[]
✅✅ line = A B C D E F G
✅✅ line = 1 2 3 4 5 6 7
✅✅ line = XYZ
✅✅ line = UFO
<_io.TextIOWrapper name='../files/words.txt' mode='r' encoding='UTF-8'>
"""
write
file = open("filename.txt", "w")
# do stuff to the file
strs = str("# CSV file ✅\nfile1, 2012\nfile2, 2016\nfile3, 2020\nfile4, 2024\n# begin\n#############################\n10 10 10 10 10 10 10 10 10 10\n20 20 20 20 20 20 20 20 20 20\n30 30 30 30 30 30 30 30 30 30\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n# ends")
file.write(strs)
"""
# ❌ String literal is unterminated Pylance
file.write("# CSV file
file1, 2012
file2, 2016
file3, 2020
file4, 2024
# begin
#############################
10 10 10 10 10 10 10 10 10 10
20 20 20 20 20 20 20 20 20 20
30 30 30 30 30 30 30 30 30 30
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
# ends
")
"""
file.close()
# viewer
file = open("filename.txt", "r")
content = file.read()
print(content)
file.close()
"""
$ py3 ./files-write.py
# CSV file ✅
file1, 2012
file2, 2016
file3, 2020
file4, 2024
# begin
#############################
10 10 10 10 10 10 10 10 10 10
20 20 20 20 20 20 20 20 20 20
30 30 30 30 30 30 30 30 30 30
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
# ends
"""
demos
python3 read and write binary
file
r+
Open the file for reading and writing both
# Take a number N as input and write the numbers 1 to N to the file "numbers.txt", each number on a separate line.
n = int(input())
# r+ => read and write ✅
# w+ => write and read ✅
file = open("../files/numbers.txt", "w+")
strs = ""
m = n
i = 1
while (m > 0):
if(i < n):
strs += str(i) + "\n"
else:
strs += str(i)
i += 1
m -= 1
file.write(strs)
file.close()
# viewer
file = open("../files/numbers.txt", "r")
content = file.read()
print(content)
file.close()
"""
$ py3 ./files-write-filing-up-with-numbers.py
"""
bytearray([source[, encoding[, errors]]])
https://docs.python.org/3.3/library/functions.html#bytearray
refs
https://thepythonguru.com/python-how-to-read-and-write-files/
https://www.tutorialspoint.com/how-do-i-read-or-write-binary-data-in-python
https://stackoverflow.com/questions/18367007/python-how-to-write-to-a-binary-file
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17589164.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
2022-07-28 CCTV《航拍中国》系列视频 All In One
2022-07-28 看了这篇使用 dist 发布 npm 包的文章,我整个人都栓Q 了
2021-07-28 css env All In One
2021-07-28 微信小程序如何使用 iconfont All In One
2021-07-28 uni-app 如何使用 icon / iconfonts All In One
2021-07-28 Android MIDI All In One
2020-07-28 GitHub for VSCode