Ten common Python automation operations

Posted by sig on Thu, 03 Mar 2022 14:42:47 +0100

This article is taken from WeChat official account GitPython: Ten common Python automation operations . In case of infringement, the contact must be deleted.

1. OS module

  • Import: import os

1. Traverse the folder

  • The premise of batch operation is to traverse the folder, OS Walk traverses the folder and generates three parameters:
    • Current folder path
    • Name of the containing folder (in list form)
    • Include file name (list form)
  • The code is as follows (change the target path as required):
for dirpath, dirnames, filenames in os.walk(r'C:\\Program Files (x86)'):
    print(f'open a folder{dirpath}')  # Current folder path
    if dirnames:
        print(dirnames)  # Include folder name (list form)
    if filenames:
        print(filenames)  # Include file name (list form)
    print('-' * 10)

# Output result:
open a 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 a folder C:\\Program Files (x86)\360
['360bizhi', '360Safe', '360SD']
----------
. . . . . . 

2. Is the destination path a file

  • Given a target path path, judge whether the path is a folder or a folder path through a line of code, and use 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 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
  • Cutting string mode: path split('\')[-1]
path = r'C:\Users\Administrator\Desktop\doc\note.md'
print(path.split('\\')[-1])
# note.md

4. Create a folder

  • The code to create a folder is very common, because the new files generated often want to have a new folder to store.
dirpath = 'xxx'
os.mkdir(dirpath)
# In this way, a folder named 'xxx' will be generated in the same directory of the 'py' file
  • However, if there is a folder with the same name, an error will be reported. FileExistsError: [WinError 183] when the file already exists, the file cannot be created.: ' xxx ', in order to avoid error reporting, you can judge whether it exists before creating:
dirpath = 'xxx'
if not os.path.exists(dirpath):
    os.mkdir(dirpath)

5. Get desktop path

  • Getting the desktop path is very common. You can use OS path. Join (OS. Path. Expanduser ("~"),'desktop ') gets the absolute path of the desktop

  • Benefits: put the data on the desktop and call the code to process the data on different computers.

  • If the desktop path is fixed in the string on one computer, the desktop path must be modified for another computer

desktop_path = os.path.join(os.path.expanduser("~"), 'Desktop')
print(desktop_path)
# C:\Users\Administrator\Desktop
  • Encapsulated into a function to call
def get_desktop_path():
    return os.path.join(os.path.expanduser("~"), 'Desktop')

6. Rename files / folders

  • Use OS Rename() method
os.rename('xxx', 'xxx2')  # Rename folder 
os.rename('test.txt', 'test2.txt')  # rename file

7. Batch file - 1

  • Except OS In addition to walk, OS. Net can also be used when non traversing folders at all levels Scandir() obtains all or qualified files in the specified path, and uses the for loop to obtain the name and path of the loop variable:
path = '.'
for file in os.scandir(path):
    print(file.name, file.path)

# Output result:
aaa .\aaa
os modular.py .\os modular.py
test2.txt .\test2.txt
xxx2 .\xxx2
# If the path is an absolute path, the next printed path is also an absolute path

8. Batch file - 2

  • Get all or qualified files in the specified path. The second method uses OS Listdir() get file name:
path = r'F:\python\python-basic\tips\Automated common operations'
for file in os.listdir(path):
    print(file)

# Output result:
aaa
os modular.py
test2.txt
xxx2

2. shutil module

9. Move files / folders (and rename)

  • Shutil is often used to move files / folders. Use shutil Move() method:
import shutil

# Put the shutil in the current directory_ test. Txt file is moved to the bbb folder of the current directory
shutil.move(r'.\shutil_test.txt', r'.\bbb/')

# Put the shutil in the current directory_ test2. Txt file is moved to the bbb folder of the current directory and renamed as shutil_test22.txt
shutil.move(r'.\shutil_test2.txt', r'.\bbb/shutil_test22.txt')

3. globa module

10. Batch file - 3

  • The most important function of golb module is to search and obtain qualified files (absolute path) under the same level or each child level, which is very suitable for writing batch code.

  • Carry out the same operation on a large number of files. After writing the operation for one file, you can complete the batch processing of all files by adding a few lines of code

  • Parameter: * indicates any character length**/* Indicates that wildcards are used to refer to any layer under a given path; If recursive is True, traversal search is allowed. The default is False

import glob

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

# Output result:
aaa
bbb
glob modular.py
os modular.py
shutil modular.py
test2.txt
xxx2
bbb\shutil_test.txt
bbb\shutil_test22.txt
  • glob can obtain the absolute path of the file under the specified path, and can also accept wildcard search, which widens the degree of flexibility.

Several uses of glob

  • The most important function of glob is to search and obtain qualified files (absolute path) under the same level or each child level.

  • Import: import glob

  • demo1: get all files, folders and their files in the current directory

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

# Output result:
aaa
bbb
glob modular.py
os modular.py
shutil modular.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 in the current directory bbb and their one-tier sub files
for file in glob.glob('./bbb/*'):
    print(file)

# Output result:
./bbb\shutil_test.txt
./bbb\shutil_test22.txt
./bbb\w
  • Get all files in the current directory and demobb3
for file in glob.glob('./bbb/**', recursive=True):
    print(file)

# Output result:
./bbb\
./bbb\shutil_test.txt
./bbb\shutil_test22.txt
./bbb\w
./bbb\w\aaaa.txt
./bbb\w\s
./bbb\w\s\i.txt
  • demo4: get the files and folders nested within two layers under the current directory bbb
for file in glob.glob('./bbb' + '/*/*'):
    print(file)

# Output result:
./bbb\w\aaaa.txt
./bbb\w\s
  • demo5: traverses files and folders with the specified name
import os

arm_path = glob.glob(os.path.join('./bbb', '*.txt'))
print(arm_path)
# Output result:
['./bbb\\shutil_test.txt', './bbb\\shutil_test22.txt']

Topics: Python Operating System