How to modify tens of thousands of file names with one key in Python

Love to learn 2021-02-22 23:59:58
modify tens thousands file names


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 :

  1. 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 .

  2. 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)

版权声明
本文为[Love to learn]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/02/20210222152149046f.html

  1. 使用Python开发DeFi项目
  2. python 函数详解
  3. Python工程师是做什么的?前景如何?
  4. Python - zip() 函数
  5. 30 周年生日,Python 先驱是怎么评价这门语言的?
  6. python将excel自适应导入数据库
  7. 从小白到大师,这里有一份Pandas入门指南
  8. [Python] 茎叶图和复合饼图的画法
  9. [Python interface automation] - regular use case parameterization
  10. Translation: practical Python Programming 02_ 02_ Containers
  11. Two years of Java, to write Python and go
  12. Translation: practical Python Programming 02_ 02_ Containers
  13. Two years of Java, to write Python and go
  14. Python-geoplot 空间核密度估计图绘制
  15. Python-seaborn 经济学人经典图表仿制
  16. python空间绘图- regionmask掩膜操作示例
  17. Python 空间绘图 - Cartopy 经纬度添加
  18. Python-pykrige包-克里金(Kriging)插值计算及可视化绘制
  19. Python 批量重采样、掩膜、坡度提取
  20. python - 多种交通方式可达圈分析
  21. Python 空间绘图 - 房价气泡图绘制
  22. Translation: practical Python Programming 02_ 02_ Containers
  23. Research on Portfolio Optimization Based on particle swarm optimization
  24. Ubuntu deploying Django project
  25. Two years of Java, write Python and go without byte beating
  26. Translation: practical Python Programming 02_ 02_ Containers
  27. So learn python, grandfather learned! Introduction to super simple Python
  28. python3 多线程 与 mongo亿级消费日志数据 新鲜demo 【优化第一版】
  29. Summary of Chinese word segmentation based on Jieba
  30. I've heard it n times, but I'm not impressed. After reading this, you'll understand
  31. Summary of Chinese word segmentation based on Jieba
  32. From movie art to Python code to realize God's reverse thinking mode
  33. Summary of Chinese word segmentation based on Jieba
  34. ARIMA模型预测CO2浓度时间序列-python实现
  35. Python belongs to back-end development or front-end development? Introduction to Python!
  36. python isinstance()
  37. I've heard it n times, but I'm not impressed. After reading this, you'll understand
  38. This article will familiarize you with the transformation process of Python - & gt; cafe - & gt; om model
  39. 如何用Python一键修改上万个文件名
  40. One day quick start to Python
  41. Python 学习笔记: List
  42. 翻译:《实用的Python编程》02_03_Formatting
  43. Is there any age requirement for learning Python? Is 30 OK?
  44. Professor Tsinghua! The most complete Python tutorial in 12 hours (free sharing at the end of the article)
  45. Using Python to develop defi project
  46. Detailed explanation of Python function
  47. Python 可变类型作为函数默认参数时的副作用
  48. What do Python engineers do? What's their future?
  49. 这是我见过最好的Python教程:十分钟带你认识Python
  50. Python欢喜冤家:爬虫与反爬虫带着处理方案来给大家拜年了
  51. Python - zip() function
  52. 写Python会遇到如下的错误:ModuleNotFoundError: No module named 'email.mime'; 'email' is not a package
  53. Python类的调用以及私有和公有属性方法的调用
  54. Python类的专有方法
  55. Python基础之:数字字符串和列表
  56. How did Python pioneers evaluate this language on their 30th birthday?
  57. Python基础之:数字字符串和列表
  58. Python基础之:数字字符串和列表
  59. 窥探未来不是梦,python数据分析轻松实现
  60. This article will familiarize you with the transformation process of Python - & gt; cafe - & gt; om model