Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python to realize batch processing and folder operation of windows

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use python to achieve windows batch processing and folder operation, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. Batch processing

The so-called batch processing is the batch processing of commands in cmd.

Python needs to import the os library to achieve the batch function, and then use the batch command to os.system (cmd_line), where cmd_line is the command in the input cmd.

Import os# batch exe file EXE_PATH= "C:\ Users\\ AAA\\ Desktop\\ 0001\\ TAppDecoder.exe" h365 TAppDecoder.exe dir= "D:\\ YUVView\\ media_h365.h365" outyuv_dir= ".yuv" # using the identifier% s, and "% s" represent the exe file to be run and the path # cmd_line to be entered for cmd (where exe And path imported by identifier) cmd_line='%s-b "% s"-o "% s"% (EXE_PATH,h365_dir,outyuv_dir) # ret is 0 proves that cmd is successful, is 1 that it failed ret=os.system (cmd_line) 2. Folder Action 2.1 read the file name in the file

To read the file names in the file, you can read all the file names in the path path directly with for file in os.listdir (path). If you need to remove the suffix of the file name, you first need to enter from pathlib import Path to import the Path library, and then enter Path (file_path) .stem, where file_path is the path to the file.

Path imported by import os#, you can read the file name after the suffix is removed and save the file directory that from pathlib import Path # needs to read path= "*" somethings=os.listdir (path) for file_1 in somethings: # if the windows system uses "\" if the linux system uses "/" Note: take the windows system as an example # here is the subdirectory path path_1=path+ "\" + file_1 somethings_1=os.listdir (path_1) # r that updates the directory path to path if there are only two-tier directories to this location If there are three or more layers, you need to increase the number of iterations. For file_2 in somethings: # at this time, file_2 is a file with a suffix name. File_2 file_2path=path_1+ "\" + file_2 # to get the name of file_2 (no suffix) the filename type is str filename=Path (file_2path). Stem # take the first two characters of filename filename [0:1] 2.2 to create a folder

First, use os.path.exists (path) to determine whether it exists (return 0 means it does not exist), and create a folder using os.makedirs (path).

Import os# if there is no path folder Just create a folder path= "*" if not os.path.exists (path): os.makedirs (path) 2.3. get a list of all files in a specified directory def getFileList (p): P = str (p) if packs = "": return [] p = p.replace ("/") "\\") if p [- 1]! = "\\": P = p + "\\" a = os.listdir (p) b = [x for x in an if os.path.isfile (p + x)] return bprint getFileList ("C:\") 2.4.decompose a path name into directory name and file name fpath Fname = os.path.split ("the path you want to decompose")

For example:

A, b = os.path.split ('ZC/Download/data/zc.txt') # split the print aprint b with the last / as the boundary

Display:

ZC/Download/data

C.txt

After reading the above, do you have any further understanding of how to use python to achieve batch processing and folder manipulation of windows? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report