Python Files All In One

xgqfrms / 2023-07-29 / 原文

Python Files All In One

open, read, write, append, binary, close

# 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


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

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.


# 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


"""

demos

refs



©xgqfrms 2012-2021

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!