Headache of modifying the file name , I was stupid My colleague asked me if I would change the file name in the directory , I yelled : You are stupid , You can't right-click to rename , Or press F2 Shortcut key , But when I unzip my colleague's directory , People are stupid , Look at the picture for yourself !!
Tell you in secret , There's a lot of such documents , If you right-click, it's going to take a day , Suddenly I didn't dare to make a sound .
Let's first look at the needs of our colleagues : Help him get rid of something like **[ Turing Programming Series ].** The word , Just the title of the book
See this , How did you solve this problem , Welcome to make complaints about Tucao , Ha ha ha ha ha ha .
Aha! , Seconds to solve the problem of colleagues When I think about how to solve the problem of colleagues , There was a flash in my mind , Previously wrote a key to modify the size of the picture program , Then why don't I write a program to modify the file name with one click !!! Do as you say
Considering the timeliness , I used it Python To write this little program , Code up :
import os
import re
import time
""" Selectively modify the name of all files in the specified directory """
def ReFileName(dirPath,pattern):
"""
:param dirPath: Folder path
:param pattern: Regular match pattern
:return:
"""
# Traverse the files in the directory
for file in os.listdir(dirPath):
# Determine if it's a document
if os.path.isfile(os.path.join(dirPath, file)) == True:
# Use regular matching , Get rid of unnecessary words
newName = re.sub(pattern, "", file)
# Set a new file name
newFilename = file.replace(file, newName)
# rename
os.rename(os.path.join(dirPath, file), os.path.join(dirPath, newFilename))
print(" The file name has been modified successfully ")
if __name__ == '__main__':
timeStart = time.time()
dirPath = r"F:\test"
pattern = re.compile(r'\[{1}(.+)]\.')
ReFileName(dirPath,pattern)
timeEnd = time.time()
print(" The program is gone %d second "%(timeEnd-timeStart))
A brief description of the code :
-
Because the requirement here is simply to modify the file name , So guys, don't use open()…… This method , Although it can be , But because there are too many files , Reading and writing is too slow , Time is very low .
-
There are also file names that need to be replaced or extracted , Not just as shown in the figure , You can use regular expressions flexibly to meet your needs .
See the effect :
Let's look at the running time of the program :
Here's another dry product for you guys !!python Get some information in the current directory .
""" Get the information in the current directory """
def ReFileName1(dirPath):
for root, dirs, files in os.walk(dirPath):
print(root) # Current directory path
print(dirs) # All subdirectories under the current path
print(files) # All non directory sub files in the current path
Okay , So much for this article , I don't want to leave a compliment when I see this , It's hard to say !! ha-ha ~ author : The cowherd learns to program
link :https://blog.csdn.net/qiukui111/article/details/106160745
[ Take it with you !Python 3.9 Official Chinese documents , Time limited collection !] (http://dwz.date/dE6v)
[ Time limit ! Quick collar !14 Zhang HD Python Quick reference table , It is necessary to improve efficiency !] (http://dwz.date/dE6w)
[GitHub Star sign 3W+,80 individual Python Case study , Take you easy to play Python Study !] (http://dwz.date/dE64)