使用子文件夹标记重命名子文件夹中的文件

2024-09-28 19:06:50 发布

您现在位置:Python中文网/ 问答频道 /正文

目前,我的文件夹中有38个子文件夹test子文件夹名称从0138。每个子文件夹有2个wav文件,它们是随机命名的,我想正确地按顺序重命名它。 例如: 子文件夹01包含wav文件My recording#1My recording#6,我希望将它们重命名为01_test#0101_test#02,因此最后一个文件夹38应该包含文件38_test#0138_test#02

下面是我的代码

import os
name = 'test'

rootdir = r'C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test'

for subdir, dirs, files in os.walk(rootdir):
    for file in files:

        filepath = subdir+os.sep +file
        if filepath.endswith('.wav'):

            split_dir = subdir.split(os.sep)

            f_name, f_ext=(os.path.splitext(file))

            new_1 = split_dir[8]
            y=1
            while y < 3 :
              new_name= (new_1 +'_' + 'test_' + str(y).zfill(2) + f_ext)
              y = y +1
              print (filepath)
              print (subdir+os.sep+new_name)
              os.rename(filepath, subdir+os.sep+new_name)

然而,当os.rename被执行时,我得到以下错误

Traceback (most recent call last):
  File "C:\Users\kushal\Desktop\final_earthquake\sikkim_demo\demo_sikkim_victor\sort_inner_wav.py", line 23, in <module>
    os.rename(filepath, subdir+os.sep+new_name)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Users\\kushal\\Desktop\\final_earthquake\\demonstration_sikkim\\wav\\test\\01\\My recording #5.wav' -> 'C:\\Users\\kushal\\Desktop\\final_earthquake\\demonstration_sikkim\\wav\\test\\01\\01_test_02.wav'

最有可能的是,它试图重命名同一个文件两次,而不是从子文件夹中重命名每个文件一次

输出:

C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\01\My recording #1.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\01\01_test_01.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\01\My recording #1.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\01\01_test_02.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\01\My recording #6.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\01\01_test_01.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\01\My recording #6.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\01\01_test_02.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\02\My recording #3.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\02\02_test_01.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\02\My recording #3.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\02\02_test_02.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\02\My recording #4.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\02\02_test_01.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\02\My recording #4.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\02\02_test_02.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\03\My recording #5.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\03\03_test_01.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\03\My recording #5.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\03\03_test_02.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\03\My recording #6.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\03\03_test_01.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\03\My recording #6.wav
C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test\03\03_test_02.wav

Tags: nametest文件夹osmyusersfinaldesktop
2条回答

您有内部while循环,导致问题,它将多次使用相同的filepath,替换为if条件,如下所示:

import os
name = 'test'

rootdir = r'C:\Users\kushal\Desktop\final_earthquake\demonstration_sikkim\wav\test'

for subdir, dirs, files in os.walk(rootdir):
    y=1
    for file in files:
        filepath = subdir+os.sep +file
        if filepath.endswith('.wav'):

            split_dir = subdir.split(os.sep)
            print split_dir

            f_name, f_ext=(os.path.splitext(file))

            new_1 = split_dir[7]

            new_name= (new_1 +'_' + 'test_' + str(y).zfill(2) + f_ext)
            y+=1
            if y>3:
                break
            print (filepath)
            print (subdir+os.sep+new_name)
            os.rename(filepath, subdir+os.sep+new_name)

你有两个for循环,这是可以理解的,但为什么有一个内部while循环?我猜,您添加while循环是因为您提到,Each sub folder has 2 wav files

第二个for循环(for file in files:)和内部while循环(while y < 3:)实际上是导致错误的原因。第二个for循环已经遍历了所有文件,因此,不需要内部while循环

在不使用内部while循环的情况下修改程序,如下所示

import os
rootdir = './test'

for subdir, dirs, files in os.walk(rootdir):
    y = 1
    for file in files:
        filepath = subdir + os.sep + file
        if filepath.endswith('.wav'):
            split_dir = subdir.split(os.sep)
            f_name, f_ext = os.path.splitext(file)
            new_name= split_dir[len(split_dir) - 1] +'_' + 'test_' + str(y).zfill(2) + f_ext
            y = y + 1
            print (filepath)
            print (subdir + os.sep + new_name)
            os.rename(filepath, subdir + os.sep + new_name)

它输出(在我的场景中):

./test\01\yy.wav
./test\01\01_test_01.wav
./test\01\xx.wav
./test\01\01_test_02.wav
./test\02\yy.wav
./test\02\02_test_01.wav
./test\02\xx.wav
./test\02\02_test_02.wav

相关问题 更多 >