Skip to main content
 首页 » 编程设计

python之如何按名称查找特定子文件夹并重命名

2024年12月31日13Terrylee

我在 D:/ 中有一个文件夹,其结构如下:

folder 
        \ tic\file0.jpg 
        \ tic\file1.jpg 
        \ tic\ tik\ file2.png 
        . 
        . 
        . 
        \ tik\xxx.png 
        \ tik\yyy.jpg 
        \ tik\zzz.png 
        . 
        . 
        . 

我想从D:/folder 中找到所有名称为tik 的子文件夹,并将其重命名为tok。我如何在 Python 中执行此操作?

预期结果:

folder 
        \ tic\file0.jpg 
        \ tic\file1.jpg 
        \ tic\ tok\ file2.png 
        . 
        . 
        . 
        \ tok\xxx.png 
        \ tok\yyy.jpg 
        \ tok\zzz.png 
        . 
        . 
        . 

到目前为止,我已经尝试了以下代码,但它不起作用:

import os 
import os.path 
 
dir = os.getcwd() 
old = "tik" 
new = "tok" 
for parent, dirnames, filenames in os.walk(dir): 
    for filename in filenames: 
        if filename.find(old)!=-1: 
            newName = filename.replace(old, new) 
            print(filename, "---->", newName) 
            os.rename(os.path.join(parent, filename), os.path.join(parent, newName)) 

更新:

我创建了一个名为 file 的假文件夹结构,如下所示:

我想用下面的代码将 tic 重命名为 toc:

import os 
from pathlib import Path 
path = r"C:\Users\User\Desktop\file"       # Path to directory that will be searched 
 
old = "tic" 
new = "toc" 
for root, dirs, files in os.walk(path, topdown=False):      # os.walk will return all files and folders in your directory 
    for name in dirs:                                       # we are only concerned with dirs since you only want to change subfolders 
        directoryPath = os.path.join(root, name)            # create a path to subfolder 
        if old in directoryPath:                            # if the word tik is found in your path then 
            parentDirectory = Path(directoryPath).parent    # save the parent directory path 
            os.chdir(parentDirectory)                       # set parent to working directory 
            os.rename(old, new)  

但我得到错误:

Traceback (most recent call last): 
  File "<ipython-input-10-2c40676a9ed9>", line 13, in <module> 
    os.rename(old, new) 
FileNotFoundError: [WinError 2] System can not find file. : 'tic' -> 'toc' 

请您参考如下方法:

这似乎运作良好,可以处理主目录中的所有子目录。如果目录中已有一个名为您打算将其更改为的文件夹,您将需要添加一些错误处理,但这应该可以让您继续。

import os 
from pathlib import Path 
path = r"Enter_path_to_directory_you_want_to_search"       # Path to directory that will be searched 
 
old = "tik" 
new = "tok" 
for root, dirs, files in os.walk(path, topdown=False):      # os.walk will return all files and folders in your directory 
    for name in dirs:                                       # we are only concerned with dirs since you only want to change subfolders 
        directoryPath = os.path.join(root, name)            # create a path to subfolder 
        if old in directoryPath:                            # if the word tik is found in your path then 
            parentDirectory = Path(directoryPath).parent    # save the parent directory path 
            os.chdir(parentDirectory)                       # set parent to working directory 
            os.rename(old, new)                             # change child folder from tik to tok