Summary of Python file and folder operations

Table of Contents

1. Overview

2. File operations

2.1 Opening and closing files

2.2 File-level operations

2.3 Operations on file contents

3. Folder operations

4. Commonly used techniques

5. Common usage scenarios

5.1 Find files of specified type

5.2 Find the file with the specified name

5.3 Find a folder with a specified name

5.4 Specify the path to find files containing the specified content

1. Overview

?In work, we often encounter operations on files and folders. When files are used a lot, using python scripts is a very convenient method. It can also achieve functions that cannot be achieved by tools such as everything and notepad++, making it more flexible. This article will introduce and demonstrate related basic operations. Various other scenarios are also combinations of basic operations.

?Note: The demonstration examples in this article are operations under window system, Python version 3.8.3, similar under Linux, mainly because there are big differences in path format and file permissions.

2. File Operation

2.1 Opening and closing of files

import os
#Open and close files
#Method 1 Use open
file=open(r"C:\Users\ZYP_PC\Desktop\verilog_test\counter\counter.v",encoding="utf-8") #Open an existing file counter. v. The encoding format opened is UTF-8. Reading garbled content in the file is most likely because the encoding format is set incorrectly. The file object is assigned to file.
print("content:", file)
file2=open(r"C:\Users\ZYP_PC\Desktop\verilog_test\counter\test.v","a") #Open the file test.v, if the file does not exist test.v will be automatically created
file.close() #Close the file
#Method 2 Use with
with open(r"C:\Users\ZYP_PC\Desktop\verilog_test\counter\counter.v") as f: #Compared with method 1, using with will automatically release resources after execution. , will not cause waste of resource usage
    print("content:",f)

The open function opens a file and the meaning of each option configuration parameter is

2.2 File-level operations

File-level operations mainly include file creation, deletion, renaming, copying, and moving.

import os
import shutil
#To create a file, use open to open the file to be created, and use the parameter w. For example, create file.txt under the specified path.
with open(r"C:\Users\ZYP_PC\Desktop\verilog_test\counter\file.txt",'w'):

#File deletion
os.remove(r"C:\Users\ZYP_PC\Desktop\verilog_test\counter\file.txt")

#Rename, rename file.txt to file_rename.txt
os.rename(r"C:\Users\ZYP_PC\Desktop\verilog_test\counter\file.txt",r"C:\Users\ZYP_PC\Desktop\verilog_test\ \counter\file_rename.txt")

#Copy files using the shutil library's copy function. For example, copy file.txt to the upper-level directory Verilog_test. If a file with the same name exists in the target path, overwrite it.
source=r'C:\Users\ZYP_PC\Desktop\verilog_test\counter\file.txt'
dest=r"C:\Users\ZYP_PC\Desktop\verilog_test"
shutil.copy(source,dest) #The update time of the copied file is the copy time
# shutil.copy(source,dest) #Can retain information such as the original creation time of the copied file

#To move files, use the move function of shutil library, such as moving file.txt to counter in the directory.
source=r'C:\Users\ZYP_PC\Desktop\verilog_test\file.txt'
dest=r"C:\Users\ZYP_PC\Desktop\verilog_test\counter"
shutil.move(source,dest) #Note that if a file already exists in the destination path, the move will fail. At this time, you can judge the file with the same name

2.3 File content operations

Common operations on file content include reading, searching, adding, deleting, and modifying

import os
import shutil
import re
#Reading file content
with open(r"C:\Users\ZYP_PC\Desktop\verilog_test\counter\counter.v",'r') as f:
    all=f.read() #Read the entire file content as a string
    print(all)
    #Read a single line character by character, defaulting to the first line
    for line in f.readline(5): #You can set the number of characters to read, such as the example of reading the first 5 characters
        print(line)
    # Read file contents line by line
    for lines in f.readlines(): #The read result f.readlines() is a list of the entire file content in line units
        print(lines)

#content search
#Specify the path to find lines containing the character module
#Method 1 Use character matching method in
pattern = 'module'
path=r'C:\Users\ZYP_PC\Desktop\verilog_test\counter'
with open(path, 'r') as file:
    for line in file:
        if pattern in line:
            print(line) #Print the line where the position is found
#Method 2, use regular matching
pattern = 'module'
path=r'C:\Users\ZYP_PC\Desktop\verilog_test\counter'
with open(path, 'r') as f:
    for line in f:
        if re.search(pattern, line):
            print(line) #Print the line where the position is found

#Content modification
#Method 1, use the replace function that comes with the character
new_str="new" #Replaced characters
old_str="old" #Original characters
path = r'C:\Users\ZYP_PC\Desktop\verilog_test\counter\counter.v'
content=""
with open(path, "r", encoding="utf-8") as f:
     for line in f:
        if old_str in line:
            line = line.replace(old_str,new_str)
        content + = line
with open(path,"w",encoding="utf-8") as f:
     f.write(content) #Write the modified content to the file

#Method 2, use the sub function in regular expressions
new_str="new" #Replaced characters
old_str="old" #Original characters
content=""
path = r'C:\Users\ZYP_PC\Desktop\verilog_test\counter\counter.v'
with open(path, "r", encoding="utf-8") as f:
     for line in f:
        if old_str in line:
            print(line)
            line=re.sub(old_str,new_str,line) #Use sub function to replace
        content + = line
with open(path,"w",encoding="utf-8") as f: #Write the modified content to the file
     f.write(content)

#Content deletion, similar to content modification, just change the new replacement character to "", and content addition is similar

3. Folder Operation

Common folder operations include create, delete, find, rename, copy, and move

import shutil
import re
from pathlib import Path
import glob

##Create a folder under the specified path
#method 1
path = r"C:\Users\ZYP_PC\Desktop\verilog_test\counter"
folder = "new_folder"
os.mkdir(os.path.join(path, folder)) #If a file with the same name already exists, an error will be reported
os.makedirs(os.path.join(path, folder),exist_ok=True) #Skip if the file with the same name already exists
#method2
path = Path(r"C:\Users\ZYP_PC\Desktop\verilog_test\counter")
folder = "new_folder"
new_folder_path = path/folder
new_folder_path.mkdir()

##Folder deletion, delete folder counter_bak
path = r"C:\Users\ZYP_PC\Desktop\verilog_test\counter_bak"
shutil.rmtree(path)

##Folder copy,
#Method 1, use shutil library, this method is recommended
new_path=r"C:\Users\ZYP_PC\Desktop\verilog_test\counter_new"
old_path=r"C:\Users\ZYP_PC\Desktop\verilog_test\counter"
if os.path.exists(new_path): #First check whether the new folder already exists. If it already exists, an error will be reported if it is copied.
    print("The folder already exists")
shutil.copytree(old_path,new_path) #Copy all files in the counter directory to counter_new. If counter_new does not exist, it will be created first.

## Folder renaming is the same as file renaming. Rename the counter folder to counter_rename.
old_name=r"C:\Users\ZYP_PC\Desktop\verilog_test\counter"
new_name=r"C:\Users\ZYP_PC\Desktop\verilog_test\counter_rename"
os.rename(old_name,new_name)

##Folder move, move the counter folder to the Desktop directory
old_path=r"C:\Users\ZYP_PC\Desktop\verilog_test\counter"
new_path=r"C:\Users\ZYP_PC\Desktop"
if os.path.exists(old_path): #First determine whether the copied folder exists.
    shutil.move(old_path,new_path)
else:
    print("The source file does not exist")

Four. Commonly used techniques

Below we will introduce some functions that are often used in file and folder operations, some of which have been covered in previous examples.

import os
import shutil
import re
from pathlib import Path
import glob
## Return to the current working directory
current_path=os.getcwd()
print(current_path)

## Determine whether the path is valid, which can be a folder path or a file path.
dir_path=r"C:\Users\ZYP_PC\Desktop\verilog_test\counter"
file_path=r"C:\Users\ZYP_PC\Desktop\verilog_test\counter\counter.v"
print(os.path.exists(file_path)) #Return true if the path is valid, otherwise return false

##Judgement of files and folders
#method 1
path=r"C:\Users\ZYP_PC\Desktop\verilog_test\counter22"
print(os.path.isfile(path)) #Determine whether the given path is a file, return True if not, return False
print(os.path.isdir(path)) #Determine whether the given path is a folder, return True if not, return False
#Method 2 Use the function Path in the pathlib library
path = Path(r'C:\Users\ZYP_PC\Desktop\verilog_test\counter')
path.is_file() #Determine whether the given path is a file, return True if not False
path.is_dir() #Determine whether the given path is a folder, return True if not, return False
#Method 3 Using the splittext function of path requires first judging the path validity.
path=r"C:\Users\ZYP_PC\Desktop\verilog_test\counter\counter_v"
if os.path.exists(path):
    print("path is valid")
    file_name, suffix = os.path.splitext(path) # splitext will return the file name and suffix. If type is not empty, it means a file, and if it is empty, it means a folder. The premise is that path exists, otherwise it will be misjudged.
    if suffix:
        print("This is a file")
    else:
        print("This is a folder")
else:
    print("path is an invalid address")

##Given a directory, return the paths of all files in the directory, and the result is a list.
path = Path(r'C:\Users\ZYP_PC\Desktop\verilog_test\counter')
files = glob.glob(os.path.join(path, '*'))
print(files)


##Separate the file name and path in the file path
dir_path=r"C:\Users\ZYP_PC\Desktop\verilog_test\counter\counter_v.mpf"
dirname,filename=os.path.split(dir_path) #dirname is C:\Users\ZYP_PC\Desktop\verilog_test\counter, filename is counter_v.mpf

##Get the file name in the file path (with suffix)
dir_path=r"C:\Users\ZYP_PC\Desktop\verilog_test\counter\counter_v.mpf"
filename=os.path.basename(dir_path) #filename is counter_v.mpf

##Separate the file name and suffix in the file path
dir_path=r"C:\Users\ZYP_PC\Desktop\verilog_test\counter\counter_v.mpf"
print(os.path.splitext(os.path.basename(dir_path))) #The result is ('counter_v', '.mpf')


##Path splicing, splicing multiple paths into one path
#Method 1, use the join function with string
path1=r"C:\Users\ZYP_PC\Desktop\verilog_test\counter"
path2="counter.v"
abs_path=os.path.join(path1,path2)
print(abs_path)
#Method 2, use pathlib’s Path function
path1=Path(r"C:\Users\ZYP_PC\Desktop\verilog_test\counter")
path2="counter.v"
abs_path=path1 / path2
print(abs_path)
#Method 3, use string to connect directly
path1=r"C:\Users\ZYP_PC\Desktop\verilog_test\counter"
path2="counter.v"
abs_path=path1 + '\' + path2 #The \ in the middle needs to be determined based on whether path1 is included or not.
print(abs_path)

##Folder traversal
#Method 1 Use os.walk function
path=r"C:\Users\ZYP_PC\Desktop\verilog_test\project_0307"
for root,dirs,file in os.walk(path): #root is the current directory, dirs is the list of all folders in the current directory, and file is the list of all files in the current directory.
    print("root:",root)
    print("dirs:",dirs)
    print("file:",file)
#Method 2 Use the os.listdir function. The difference from os.walk is that it will not traverse subdirectories. To implement recursive traversal, you need to define a function implementation.
path=r"C:\Users\ZYP_PC\Desktop\verilog_test\project_0307"
for file in os.listdir(path): #root is the current directory, dirs is the list of all folders in the current directory, and file is the list of all files in the current directory.
    abs_path=os.path.join(path,file)
    print("abs_path:",abs_path)
#Method 3 Using the glob.glob function will not traverse subdirectories. To implement recursive traversal, you need to define a function implementation.
path=r"C:\Users\ZYP_PC\Desktop\verilog_test\project_0307"
files=glob.glob(os.path.join(path,"*")) #Get all files and folders in the current directory
print("files:",files)

5. Common usage scenarios

5.1 Find files of specified type

Query all txt files in the specified directory, return the found file path, and save it in list form

import os

# Query files of the specified type in the specified directory and return the absolute path to the found result.
def find_type(path,type):
    file_find=[]
    for root,dirs,files in os.walk(path): #Get all files in the specified directory
        for file in files:
            if file.endswith(".txt"):
                file_find.append(os.path.join(root, file))
    print("files:",file_find)
path=r"C:\Users\ZYP_PC\Desktop\verilog_test\project_0307"
suffix=".txt"
find_type(path,suffix) #Take the search for all txt files under the directory project_0307 as an example

search result

5.2 Find the file with the specified name

Similar to 5.1, the main modification is to modify the judgment conditions after the if, such as searching for the counter_tb.v file in the project_0307 directory.

def find_file(path,f_name):
    file_find=[]
    for root,dirs,files in os.walk(path): #Get all files in the specified directory
        for file in files:
            if file==f_name: #Judgment condition to replace, replace with file name search
                file_find.append(os.path.join(root, file))
    print("files:",file_find)
path=r"C:\Users\ZYP_PC\Desktop\verilog_test\project_0307"
file="counter_tb.v"
find_file(path,file) #Take the counter_tb.v file in the directory project_0307 as an example

search result

5.3 Find the folder with the specified name

Take, for example, searching for all folders named sim_1 in the directory project_0307.

# Query the folder with the specified name in the specified directory and return the absolute path to the found result.
def find_dir(path,dir_name):
    folder_find=[]
    for root,dirs,files in os.walk(path): #Get all files and folders in the specified directory
        for dir in dirs:
            if dir==dir_name:
                folder_find.append(os.path.join(root, dir))
    print("find_result:",folder_find)
path=r"C:\Users\ZYP_PC\Desktop\verilog_test\project_0307"
dir_name="sim_1"
find_dir(path,dir_name) #Take the search for all folders named sim_1 under the directory project_0307 as an example

Find results

5.4 Specify the path to find the file containing the specified content

To find the log file containing the character FPGA in the directory project_0307

def find_file(path,suffix,content):
    file_find=[]
    for root,dirs,files in os.walk(path): #Get all files in the specified directory
        for file in files:
            if file.endswith(suffix): #Judge the condition and replace it with file name search
                abs_path=os.path.join(root, file)
                with open(abs_path,"r") as f:
                    for line in f:
                        if content in line:
                            file_find.append(abs_path)
    print("files:",file_find)
path=r"C:\Users\ZYP_PC\Desktop\verilog_test\project_0307" #Search directory
suffix=".log" #The file type searched is log type
content="FPGA" #The file contains the character FPGA
find_file(path,suffix,content) #Take the counter_tb.v file in the directory project_0307 as an example

Find results