【问题标题】:How to find specific subfolders by name and rename them?如何按名称查找特定子文件夹并重命名?
【发布时间】:2019-01-15 03:32:00
【问题描述】:

我在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'

【问题讨论】:

    标签: python operating-system file-rename


    【解决方案1】:

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

    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   
    

    【讨论】:

    • 谢谢,我尝试将 'tic' 替换为 'tok',但出现 FileNotFoundError,你知道为什么吗?
    • @ahbon 你能发布你使用的引发错误的代码吗?
    • 好像只能重命名最后一级子目录没有错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 2020-10-21
    相关资源
    最近更新 更多