xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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

image


# 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'>

"""


image

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

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

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, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-07-28 23:53  xgqfrms  阅读(15)  评论(3编辑  收藏  举报