It's a simple program, but I used OCTA, and sometimes I want to extract only UDF files and process the file names in a batch, so I made it as a reminder.
re
: Regular expression module
Use re.compile ()
to compile the strings in it into a regular expression to create an object.
reg_exit = re.compile (keyword)
is a regular expression by specifying a keyword to compile into a regular expression with compile
. Put this object in reg_exit
.
re.search ()
: All strings are searched.
ʻOs: Can operate files and directories. ʻOs.rename (a, b)
: Rename ʻato
b`
With for name in file_list:
, extract the file names one by one and put them in name
.
ʻIf reg_exit.search (name):determines if there is a compiled keyword in the regular expression in the extracted file name. ʻUdf_file.append (name)
appends the file with the keyword to the list.
ʻOs.rename (file, category + file) adds the keyword (
category) to the file name (
file`)
Add a classification name to all file names in OCTA
#Import module
import os
import re
#Make a list of files in the directory.
file_list = os.listdir()
udf_file = []
reg_exit = re.compile(r'(.udf)$')
for name in file_list:
if reg_exit.search(name):
udf_file.append(name)
#Specify the classification name (category) and add it to the file name (os).rename)
category = 'Category 10_'
for file in udf_file:
os.rename(file, category+file)
Recommended Posts