简体   繁体   English

Python脚本以递归方式重命名文件夹和子文件夹中的所有文件

[英]Python script recursively rename all files in folder and subfolders

Hi I have a number of different files that need to be renamed to something else. 嗨,我有许多不同的文件需要重命名为其他文件。 I got this far but I want to have it so that I can have many items to replace and their corresponding replacements rather than type each one out, run the code then retype it again. 我已经走了这么远,但我想拥有它,以便我可以替换许多项及其对应的替换项,而不必逐一键入,运行代码然后再次键入。

UPDATE* Also I need the rename to only change part of the file not the whole thing so if there was a "Cat5e_1mBend1bottom50m2mBend2top-Aqeoiu31" it would just change it to "'Cat5e50m1mBED_50m2mBE2U-Aqeoiu31" UPDATE *另外,我还需要重命名以仅更改文件的一部分而不是整个文件,因此,如果存在“ Cat5e_1mBend1bottom50m2mBend2top-Aqeoiu31”,则只需将其更改为“'Cat5e50m1mBED_50m2mBE2U-Aqeoiu31”

import os, glob

#searches for roots, directory and files
for root,dirs, files in os.walk(r"H:\My Documents\CrossTalk\\"):
   for f in files:
       if f == "Cat5e_1mBend1bottom50m2mBend2top":#string you want to rename
          try:
             os.rename('Cat5e_1mBend1bottom50m2mBend2top', 'Cat5e50m1mBED_50m2mBE2U'))
          except FileNotFoundError, e:
             print(str(e))

Is this wath you want? 这是你想要的吗?

import os, glob
#searches for roots, directory and files
#Path
p=r"C:\\Users\\joao.limberger\\Documents\\Nova Pasta"
# rename arquivo1.txt to arquivo33.txt and arquivo2.txt to arquivo44.txt
renames={"arquivo1.txt":"arquivo33.txt","arquivo2.txt":"arquivo44.txt"}
for root,dirs,files in os.walk(p):
   for f in files:
      if f in renames.keys():#string you want to rename
         try:
            os.rename(os.path.join(root , f), os.path.join(root , renames[f]))
            print("Renaming ",f,"to",renames[f])
         except FileNotFoundError as e:
            print(str(e))

Check if this is wath you want!!! 检查这是否是您想要的水!

import os, glob
#searches for roots, directory and files
#Python 2.7
#Path 
p=r"C:\\Users\\joao.limberger\\Documents\\Nova Pasta"
#  if the substring in the key exist in filename, replace the substring 
#   from the value of the key
#   if the key is "o1" and the value is "oPrinc1" and the filename is
#  arquivo1.txt ... The filename whil be renamed to "arquivoPrinc1.txt"
renames={"o1":"oPrinc1","oldSubs":"newSubs"}
for root,dirs,files in os.walk(p):
    for f in files:
        for r in renames:
            if r in f:
                newFile = f.replace(r,renames[r],1)
                try:
                    os.rename(os.path.join(root , f), os.path.join(root , newFile))
                    print "Renaming ",f,"to",newFile
                except FileNotFoundError , e:
                    print str(e)

The first thing you'd need is a dictionary for the replacements, then a small change in your code: 您需要的第一件事是替换dictionary ,然后在代码中进行一些小的更改:

import os, glob

name_map = {
     "Cat5e_1mBend1bottom50m2mBend2top": 'Cat5e50m1mBED_50m2mBE2U'
}

#searches for roots, directory and files
for root,dirs,files in os.walk(r"H:\My Documents\CrossTalk"):
   for f in files:
       if f in name_map:
          try:
             os.rename(os.path.join(root, f), os.path.join(root, name_map[f]))
          except FileNotFoundError, e:
          #except FileNotFoundError as e:  # python 3
             print(str(e))

In the name_map, the key (string to the left of " : ") is the name of the file in your filesystem, and the value (string to the right of the " : ") is the name you want to use. 在name_map中, key (“ : ”左侧的字符串)是文件系统中文件的名称, value (“ : ”右侧的字符串)是您要使用的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Python 重命名文件夹中的所有文件 - Rename all files in folder with Python python脚本以获取多个子文件夹中的所有.mp3文件并移至PC上的一个文件夹? - python script to get all .mp3 files in mulitiple subfolders and move to one folder on a pc? 我想用 python 重命名文件夹中的所有文件 - I want to rename all the files in a folder with python 在所有子文件夹中递归执行Python程序 - Recursively execute a Python program in all subfolders 使用python脚本将一个文件夹中的多个jpeg文件和json文件复制并排序到子文件夹文件夹中的另一个文件夹 - copy and sort multiple jpeg files and json files in one folder to another folder in subfolders folder using python script 在Python中按特定格式重命名子文件夹中的文件 - Rename files in subfolders by specific format in Python 使用 python 和 os.walk() 重命名子文件夹中的文件 - Rename files in subfolders with python and os.walk() python:通过子文件夹名称重命名子文件夹中的文件 - python: rename files in subfolders by subfolder name 更改文件夹,所有子文件夹和所有文件的权限 - Change permissions for folder, all subfolders, and all files 在 Python 中递归生成文件夹中所有文件的绝对路径的最快方法? - Fastest way to generate the absolute paths of all the files in a folder recursively in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM