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

问题描述 投票:2回答:1

我在D:/有一个文件夹有以下结构:

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

我想从tik找到所有名为D:/folder的子文件夹,并将其重命名为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的假文件夹结构,如下所示:

enter image description here

我想用以下代码将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个回答
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   
© www.soinside.com 2019 - 2024. All rights reserved.