How Python operates on file directories

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:

  1. os.path.getsize is used to get the size of the specified file, the unit is Byte.
  2. round is a rounding function, keeping the specified number of decimals.
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:

  1. max_file = max(file_dict, key=lambda x: file_dict[x])
  2. min_file = min(file_dict, key=lambda x: file_dict[x])
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: **

  1. Read the contents of the two files and parse them to get the key and value, and store them in the dictionary (two dictionaries, respectively store the contents of the two files).
  2. Traverse the first dictionary and find the value of the same key in the second dictionary. If it does not exist, only the first content will be displayed. If it exists, it will be merged.
  3. Traverse the second dictionary and find the value of the key that is not in the first dictionary, and accumulate it.
  4. Save the spliced list into the file.

Core code

Define a function read_book to read the contents of two files. The key points are as follows:

  1. The open function is used to open a file, and the file encoding is UTF-8.
  2. readlines is used to read all the lines and return a list.
  3. split is used to split a string into an array.
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:

  1. append is used to add new elements to an array.
  2. The dict.keys function is used to return all keys.
  3. The join function is used to convert an array into a string and divide it by corresponding characters.
  4. writelines is used to write all lines to the file.
  5. With syntax, when the execution ends, it will automatically close and release resources.
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

How Python operates on file directories
How to write python configuration file
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to install Python on CentOS 8
How to install Python2 on Ubuntu20.04 ubuntu/focal64
python_ file processing
How to upgrade all Python libraries on Ubuntu 18.04
How to program based on interfaces in Python
How to play happily with Python3 on Ubuntu
[Practice] How to install python3.6 on Ubuntu system
How to switch the hosts file using python
How to delete files and directories in python
How to deal with python file reading failure
How to practice after the python file is written
Install Python3 on Ubuntu 14.04
Install Python3 on Ubuntu 16.04
How python was invented
Install Python3.7 on Ubuntu
python Tic-Tac-Toe-text version (on)
How Python parses XML
How to build an NFS file sharing server on Ubuntu 16.04
How to comment python code
How Python converts string case
Install Python 3.7 on Ubuntu 18.04 LTS
How does python output backslashes
Python on image processing PIL
Python implements TCP file transfer
How to learn python quickly
How to uninstall python plugin
Install python3 on linux, keep python2
Python tornado upload file function
How Python implements FTP function
How to understand python objects
How to use python tuples
Quick start Python file operation
How does python update packages
Explain how python references package package