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 modify tens of thousands of file names with one click of Python

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to use the Python button to modify tens of thousands of file names", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use the Python button to modify tens of thousands of file names"!

For timeliness, I used Python to write this Mini Program, code: import osimport reimport time

"" selectively change the name of all files in the specified directory "def ReFileName (dirPath Pattern): ": param dirPath: folder path: param pattern: regular matching pattern: return:" # traversing the files in the directory for file in os.listdir (dirPath): # determine whether it is a file if os.path.isfile (os.path.join (dirPath, file)) = = True: # use regular matching Remove the unwanted words newName = re.sub (pattern, ", file) # set the 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 ("Program took% d seconds"% (timeEnd-timeStart))

A brief description of the code:

1. Because the requirement here is to simply change the file name, so friends must not use open (). This method, although it is also possible, but because there are too many files, reading and writing is too slow, the time limit is very low.

two。 There are also filenames that need to be replaced or extracted, not just those shown in the figure, but you have the flexibility to use regular expressions to meet your needs.

Depending on the effect:

Then look at the running time of the program:

Here is another piece of practical information to share with our friends! Pyt hon gets some information under 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 subfiles under the current path

Thank you for your reading, the above is "how to use Python button to modify tens of thousands of file names" content, after the study of this article, I believe you on how to use Python button to modify tens of thousands of file names this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report