Continue learning Python
Today is the file operation
1. Read files
1.1 the read ()
# 1. Open the file
f = open('a.txt'.'r', encoding='utf-8')
Read (n) n How many bytes are read at a time
buf = f.read(3)
print(buf) # 123
print(The '-'*30)
buf = f.read(3)
print(buf)
# 3. Close files
f.close()
Copy the code
1.2 Reading files by line
f = open('a.txt'.'r', encoding='utf-8')
# f.readline() reads one line at a time and returns what was read (STR)
# buf = f.readline()
# f.readlines() reads lines, all at once, and returns a list. Each item in the list is a string, the contents of a line
buf = f.readlines()
print(buf)
buf = [i.strip() for i in buf]
print(buf)
f.close()
Copy the code
2. Simulate reading large files
# read() Reads everything at once
# readline() returns null at the end of the file
f = open('a.txt'.'r', encoding='utf-8')
while True:
buf = f.readline()
if buf:
print(buf, end=' ')
else:
break
Copy the code
3. File opening mode
Text files: TXT,.py. Md Files that can be opened with Notepad Binary files: files with special formats, such as MP3, MP4, RMVB, AVI, PNG, JPG, etc
Text files Can be opened in text mode or binary mode
Binary files can only be opened in binary mode. Binary data is required for both reading and writing rb wb ab Note: The encoding argument cannot be specified
f = open('c.txt'.'wb')
f.write('hello'.encode()) # encode() converts STR to a string in binary format
f.close()
f1 = open('c.txt'.'rb')
buf = f1.read()
print(buf)
print(buf.decode())
f1.close()
Copy the code
4. Application – File backup
# 1. Open the file in read-only mode
f = open('a.txt'.'rb')
# 2. Read file contents
buf = f.read()
# 3. Close files
f.close()
# 4. Write only, open a new file
f_w = open('a [backup]. TXT'.'wb')
# 5. Write what you read in step 2 to a new file
f_w.write(buf)
# 6. Close the new file
f_w.close()
Copy the code
File backup – optimization (file and folder operations)
file_name = input('Please enter the name of the file you want to backup')
# 1. Open the file in read-only mode
f = open(file_name, 'rb')
# 2. Read file contents
buf = f.read()
# 3. Close files
f.close()
Find the file name suffix and file name according to the original file name
index = file_name.rfind('. ')
# file_name[index:]
# new file name
new_file_name = file_name[:index] + Backup '[]' + file_name[index:]
print(new_file_name)
# 4. Write only, open a new file
f_w = open(new_file_name, 'wb')
# 5. Write the contents read in step 2 to a new file
f_w.write(buf)
# 6. Close the new file
f_w.close()
Copy the code
5. Change the file name in batches
import os
def create_files() :
for i in range(10):
file_name = 'test/file_' + str(i) + '.txt'
print(file_name)
f = open(file_name, 'w')
f.close()
def create_files_1() :
os.chdir('test')
for i in range(10.20):
file_name = 'file_' + str(i) + '.txt'
print(file_name)
f = open(file_name, 'w')
f.close()
os.chdir('.. / ')
def modify_filename() :
os.chdir('test')
buf_list = os.listdir()
for file in buf_list:
new_file = 'py43_' + file
os.rename(file, new_file)
os.chdir('.. / ')
def modify_filename_1() :
os.chdir('test')
buf_list = os.listdir()
for file in buf_list:
num = len('py43_')
new_file = file[num:]
os.rename(file, new_file)
os.chdir('.. / ')
modify_filename_1()
Copy the code