Overview
I/O operations include not only screen input and output, but also file reading and writing. Python provides many necessary methods and functions for file and folder related operations. This article mainly uses two simple small examples to briefly describe the application of Python in folders and files, which are only for learning and sharing. If there are any deficiencies, please correct me.
Involving knowledge points
os module: The os module provides a very rich method for handling files and directories.
Open method: The open method is used to open a file for reading and writing.
Example 1: Get all the file sizes in the specified directory, and find the largest file and smallest file
**Decomposition steps: **
Traverse all sub-files and sub-folders under the folder (recursion is required), and calculate the size of each file
Calculate the total size of all files
Find the largest file and the smallest file
Core code
Define a method get_file_size to obtain the size of a single file in two units: KB and MB. The key points are as follows:
def get_file_size(file_path, KB=False, MB=False):"""Get file size"""
size = os.path.getsize(file_path)if KB:
size =round(size /1024,2)
elif MB:
size =round(size /1024*1024,2)else:
size = size
return size
Define a method list_files to traverse the specified file directory and store it in the dictionary. The key points are as follows:
def list_files(root_dir):"""Iterate over files"""if os.path.isfile(root_dir): #If it is a file
size =get_file_size(root_dir, KB=True)
file_dict[root_dir]= size
else:
# If it is a folder, traverse
for f in os.listdir(root_dir):
# Splicing path
file_path = os.path.join(root_dir, f)if os.path.isfile(file_path):
# If it is a file
size =get_file_size(file_path, KB=True)
file_dict[file_path]= size
else:list_files(file_path)
Calculate the total size and the largest and smallest files as shown below:
By comparing the size of the dictionary value, the name of the corresponding key is returned. The key points are as follows:
if __name__ =='__main__':list_files(root_dir)
# print(len(file_dict))
# Calculate file directory size
total_size =0
# Traverse the key of the dictionary
for file in file_dict:
total_size += file_dict[file]print('total size is : %.2f'% total_size)
# Find the largest and smallest files
max_file =max(file_dict, key=lambda x: file_dict[x])
min_file =min(file_dict, key=lambda x: file_dict[x])print('max file is : ', max_file,'\n file size is :', file_dict[max_file])print('min file is : ', min_file,'\n file size is :', file_dict[min_file])
Example 2: Combine the contents of two text files and save them in the file
The contents of the two files are as shown in the following figure:
**Decomposition steps: **
Core code
Define a function read_book to read the contents of two files. The key points are as follows:
def read_book():"""Read content"""
# Read a file
file1 =open('book1.txt','r', encoding='UTF-8')
lines1 = file1.readlines()
file1.close()for line in lines1:
line = line.strip() #Go blank
content = line.split(',')
book1[content[0]]= content[1]
# Another way, read another file,No need to close, it will close automatically
withopen('book2.txt','r', encoding='UTF-8')as file2:
lines2 = file2.readlines()for line in lines2:
line = line.strip() #Go blank
content = line.split(',')
book2[content[0]]= content[1]
Define a function to merge content and save it. The key points are as follows:
def merge_book():"""Merge content"""
lines =[] #Define an empty list
header ='Name\t phone\t text\n'
lines.append(header)
# Traverse the first dictionary
for key in book1:
line =''if key in book2.keys():
line = line +'\t'.join([key, book1[key], book2[key]])
line +='\n'else:
line = line +'\t'.join([key, book1[key],' *****'])
line +='\n'
lines.append(line)
# Traverse the second one, and write that is not included in the first one
for key in book2:
line =''if key not in book1.keys():
line = line +'\t'.join([key,' *****', book2[key]])
line +='\n'
lines.append(line)
# Write to book3
withopen('book3.txt','w', encoding='UTF-8')as f:
f.writelines(lines)
The overall call is as follows:
if __name__ =='__main__':
# Read content
read_book()
# Merge content
merge_book()
# print(book1)
# print(book2)
The file generated after the last splicing is as follows:
Through the above two examples, you can roughly understand some methods and steps of file and directory operations.
Recommended Posts