Python Automation Common Operation and glob Use Encyclopedia

  • 2021-11-24 02:16:28
  • OfStack

Directory 1. OS Module 2. shutil Module 3. globa Module Several Uses of glob

This article is taken from WeChat official account GitPython: 10 common operations of Python automation. If there is infringement, contact will be deleted.

1. OS module

Import: import os

1. Traverse folders

The premise of batch operation is to traverse the folder, os.walk Traversing the folder produces three parameters:

Current folder path Contains the name of the folder (in list form) Include file name (list form)

The code is as follows (just change the target path as needed):


for dirpath, dirnames, filenames in os.walk(r'C:\\Program Files (x86)'):
    print(f' Open Folder {dirpath}')  #  Current folder path 
    if dirnames:
        print(dirnames)  #  Include folder names (in list form) 
    if filenames:
        print(filenames)  #  Include file name (list form) 
    print('-' * 10)

#  Output: 
 Open Folder C:\\Program Files (x86)
['360', 'BirdWallpaper', 'Common Files', 'erl5.9.3.1', 'InstallShield Installation Information', 'Intel', 'Internet Explorer', 'IQIYI Video', 'Java', 'Kingsoft', 'Microsoft Office', 'Microsoft.NET', 'MSBuild', 'NetSarang', 'NVIDIA Corporation', 'Reference Assemblies', 'Sangfor', 'Sinfor', 'SogouInput', 'svnfile', 'Tencent', 'UltraEdit', 'VMware', 'Windows Defender', 'Windows Mail', 'Windows Media Player', 'Windows Multimedia Platform', 'Windows NT', 'Windows Photo Viewer', 'Windows Portable Devices', 'Windows Sidebar', 'WindowsPowerShell', 'Youdao']
['desktop.ini', ' Microsoft Common Runtime Collection _2019.07.20_X64.exe']
----------
 Open Folder C:\\Program Files (x86)\360
['360bizhi', '360Safe', '360SD']

2. Is the destination path a file

Given 1 destination path path Using 1 line of code to determine whether the path is a folder or a folder path, use the os.path.isfile(path) , return True Or False .


path = r'C:\Users\Administrator\Desktop\doc\note.md'
print(os.path.isfile(path))  # True
path = 'xxx'
print(os.path.isfile(path))  # False

3. Get the file name in the path

os Module Mode: os.path.basename You can get the last file name directly from the absolute path


path = r'C:\Users\Administrator\Desktop\doc\note.md'
print(os.path.basename(path))
print(path.split('\\')[-1])
# note.md

How to cut a string: path.split('\\')[-1]


path = r'C:\Users\Administrator\Desktop\doc\note.md'
print(path.split('\\')[-1])
# note.md

Step 4 Create a folder

The code for creating folders is very common, because often new files are generated that want to be stored in a new folder.


dirpath = 'xxx'
os.mkdir(dirpath)
#  This will be used in the `py` The document is the same as 1 Directory to generate a generation named `xxx` Folder of 

However, if a folder with the same name exists, an error will be reported FileExistsError: [WinError 183] 当文件已存在时,无法创建该文件。: 'xxx' In order to avoid reporting errors, you can determine whether it exists before creating:


dirpath = 'xxx'
if not os.path.exists(dirpath):
    os.mkdir(dirpath)

5. Get the desktop path

Getting the desktop path is very common and can be used os.path.join(os.path.expanduser("~"), 'Desktop') Gets the absolute path of the desktop Benefits: Put the data on the desktop, and you can call code to process the data on different computers. If the desktop path is fixed in a string on one computer, you must modify the desktop path on another computer

desktop_path = os.path.join(os.path.expanduser("~"), 'Desktop')
print(desktop_path)
# C:\Users\Administrator\Desktop

Encapsulated as a function to call


def get_desktop_path():
    return os.path.join(os.path.expanduser("~"), 'Desktop')

6. Rename files/folders using os.walk0 Method


os.rename('xxx', 'xxx2')  #  Rename Folder 
os.rename('test.txt', 'test2.txt')  #  Rename file 

7. Batch File-1

Except for os.walk In addition, when not traversing folders at all levels, you can also use os.scandir() Gets all or eligible files for the specified path, using the for Loop, getting the of the loop variable name And path :


path = '.'
for file in os.scandir(path):
    print(file.name, file.path)

#  Output result :
aaa .\aaa
os Module .py .\os Module .py
test2.txt .\test2.txt
xxx2 .\xxx2
#  If path It is an absolute path, and what is printed below is also an absolute path 

8. Batch File-2

Get all or eligible files for the specified path. The second method uses os.listdir() Get the file name:


path = r'C:\Users\Administrator\Desktop\doc\note.md'
print(os.path.isfile(path))  # True
path = 'xxx'
print(os.path.isfile(path))  # False
0

2. shutil Module

9. Move files/folders (and rename them) shutil Commonly used to move files/folders, using the shutil.move() Methods:


path = r'C:\Users\Administrator\Desktop\doc\note.md'
print(os.path.isfile(path))  # True
path = 'xxx'
print(os.path.isfile(path))  # False
1

3. globa Module

10. Batch File-3

golb The most important function of the module is to search for files (absolute paths) that meet the conditions under the same level or sub-levels, which is very suitable for writing batch code. Do the same operation for a large number of files. After writing the operation for one file, you only need to add a few lines of code to complete the batch processing of all files Parameters: * Represents any character length; **/* Indicates that wildcard characters refer to any 1 layer under a given path; recursive For True Indicates that traversal search is allowed, and the default is False

path = r'C:\Users\Administrator\Desktop\doc\note.md'
print(os.path.isfile(path))  # True
path = 'xxx'
print(os.path.isfile(path))  # False
2

glob It can get the absolute path of the file under the specified path, and can also accept wildcard search, which broadens the flexibility.

Several Uses of glob

glob The most important function is to search for files (absolute paths) that meet the conditions under the same level or children. Import: import glob demo1: Get all files and folders and their files in the current directory

for file in glob.glob('**/*', recursive=True):
    print(file)

#  Output: 
aaa
bbb
glob Module .py
os Module .py
shutil Module .py
test2.txt
xxx2
bbb\shutil_test.txt
bbb\shutil_test22.txt
bbb\w
bbb\w\aaaa.txt
bbb\w\s
bbb\w\s\i.txt

demo2: Get the files and their 1-layer sub-files under the current directory bbb


path = r'C:\Users\Administrator\Desktop\doc\note.md'
print(os.path.isfile(path))  # True
path = 'xxx'
print(os.path.isfile(path))  # False
4

demo3: Get all files and folders and their files under the current directory bbb


path = r'C:\Users\Administrator\Desktop\doc\note.md'
print(os.path.isfile(path))  # True
path = 'xxx'
print(os.path.isfile(path))  # False
5

demo4: Get the files and folders nested in two layers under the current directory bbb


path = r'C:\Users\Administrator\Desktop\doc\note.md'
print(os.path.isfile(path))  # True
path = 'xxx'
print(os.path.isfile(path))  # False
6

demo5: Traversing through files and folders containing the specified name


path = r'C:\Users\Administrator\Desktop\doc\note.md'
print(os.path.isfile(path))  # True
path = 'xxx'
print(os.path.isfile(path))  # False
7

Related articles: