Filing has to mention regular expressions first
Send link:
Basic usage of python:re module
1. List all files under the folder
Library os used
Python os.path() module in detail with an example
os.walk() usage
[(Current Directory List), (Subdirectory List), (File List)]os. Walk (tree folder name)
os.walk() returns a list of three tuple-type elements.
A table element with an index value of 0 is the folder name, from which you can know which folder is currently being processed.
A table element with an index value of 1 is a list of the next folders used to know how many lower folders there are in this folder by what name.
An element with an index value of 2 is a list of all the files in this folder, listing all the file names in this folder.
Combines the contents of all downward tree directory structures from the returned list data.

import os list_all=[]#Initialize an empty list for root ,dirs,files in os.walk(r'C:\Users\Shineion\Desktop\New Folder'): for name in files: file_path=os.path.join(root,name)#Files with paths file_name=os.path.split(file_path)[-1] list_all.append(file_name) print(list_all)

If the folder contains subfolders, the subfolders have files. The program has no effect and can read all the file names as well.
2. Use regular re to modify file names
import os import re #First define the rules, such as if we need to extract all the picture names and modify them pattern=re.compile(r'.+\.png') i=1 for root ,dirs,files in os.walk(r'C:\Users\Shineion\Desktop\New Folder'): for name in files: file_path=os.path.join(root,name)#File name containing path matching=pattern.search(file_path)#Match Picture if matching: os.rename(file_path,os.path.split(file_path)[-2]+ '/{}.png'.format(i)) i+=1 print('Successful modification')
Explain
Code | Meaning |
---|---|
os.path.split(path) | Split the path into dirname and basename and return a tuple |
os.rename | Two parameters are passed in, the old file name on the left (the file name containing the path), and the second parameter contains the new file name of the path |
os.path.split(file_path)[-2]:[-2]: is to get the picture path (remove the part of the name)

File renaming that was previously written to another version
: python batch modify picture names (can also be used to batch modify file names)
3. Bulk Delete and Copy Files
1. Bulk Copy Files
Example: We copy the PDF from the original folder to a new folder: D:\Yudenwu Test 1
First D:\Yu Tengwu Test 1, need to manually create new
import os import re #Define rules first, such as we need to put all pdf files pattern=re.compile(r'.+\.pdf') for root ,dirs,files in os.walk(r'C:\Users\Shineion\Desktop\New Folder'): for name in files: file_path=os.path.join(root,name)#Files with paths print(file_path) matching=pattern.search(file_path)#Match pdf if matching: command_line='copy %s D:\\Yu Dengwu Test 1' % file_path.replace('/','\\') os.system(command_line) print('Copy succeeded')
os.system(command_line) calls cmd to execute the copy file.
cmd copy file command:
copy Source File Destination Path

Copied files
2. Bulk Delete Files
Delete pdf under original folder
Instruction os. Remove (file)
import os import re #Define rules first, such as we need to put all pdf files pattern=re.compile(r'.+\.pdf') for root ,dirs,files in os.walk(r'C:\Users\Shineion\Desktop\New Folder'): for name in files: file_path=os.path.join(root,name)#Files with paths print(file_path) matching=pattern.search(file_path)#Match pdf if matching: os.remove(file_path) print('Delete succeeded')

Looking at the original folder, we found that the pdf file has been deleted
That's it. Send previous links to Word, Excel mail articles
python automated office: excel, no overtime from now on.
python automated office: word. A professional is not a dream.
python automated office: mail (timed mail greeting ticket so easy)
With the advent of computers in electrical specialty, blogging is not easy. If you feel this is helpful to you, please give some support. Thank you.