Manipulate OCTA udf filenames in Python

In Python, use the re and os modules to process file names in a batch.

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.

Add a classification name to the file name at once.

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 ʻatob`

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

Manipulate OCTA udf filenames in Python
Manipulate files and folders in Python
Manipulate namespaced XML in Python (Element Tree)
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Handle zip files with Japanese filenames in Python 3
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Daily AtCoder # 33 in Python
Solve ABC168D in Python
Logistic distribution in Python