Python batch modify file name template

Source as follows: Import os

Import Re
 Import SYS 


filePath = R & lt ' F.: \ BaiduNetdiskDownload \ COVID-19CTSeg \ 3DUNet-Pytorch \ label ' 
the fileList = the os.listdir (filePath)
 # display file 
DEF showfile (filePath): 
    the fileList = the os.listdir (filePath)
     for filename in fileList:
         Print (filename) 

# get the current process's working directory 
currentPath = os.getcwd ()
 # the current working directory to be amended to modify the location of the folder 
os.chdir (filePath)
 Print ( " before the amendment: / the n- " )
showfile (filePath) 
# name of the variable 
Patient_num = [1,2,4,5,6,7,8,9,10,11,12,14,15,16,18,19,21,22,23 ]
 # traversal and modify the 
# while traversing the two variables plus zip, otherwise it will error ValueError: TOO MANY values to unpack
for filename, NUM in ZIP (fileList, Patient_num): # match the file name regular expression PAT = " + \ (NII).. " # matching pattern = the re.findall (PAT, filename) os.rename (filename, ( ' Patient000 ' + STR (NUM) + ' . ' + pattern [0])) # file after the specified storage location renaming # fall back before running the working directory os.chdir (currentPath) # refresh sys.stdin.flush () Print ( " modified: / the n- " ) showfile (filePath)

Guess you like

Origin www.cnblogs.com/dyc99/p/12573687.html
Recommended