如何通过名称查找特定的子文件夹并将其重命名?

时间:2019-01-15 03:32:01

标签: python operating-system file-rename

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

1 个答案:

答案 0 :(得分: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