目前,我的文件夹中有38个子文件夹test子文件夹名称从01到38。每个子文件夹有2个wav文件,它们是随机命名的,我想正确地按顺序重命名它。 例如: 子文件夹01包含wav文件My recording#1和My recording#6,我希望将它们重命名为01_test#01和01_test#02,因此最后一个文件夹38应该包含文件38_test#01和38_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
您有内部
while
循环,导致问题,它将多次使用相同的filepath
,替换为if
条件,如下所示:你有两个for循环,这是可以理解的,但为什么有一个内部while循环?我猜,您添加while循环是因为您提到,
Each sub folder has 2 wav files
第二个for循环(
for file in files:
)和内部while循环(while y < 3:
)实际上是导致错误的原因。第二个for循环已经遍历了所有文件,因此,不需要内部while循环在不使用内部while循环的情况下修改程序,如下所示
它输出(在我的场景中):
相关问题 更多 >
编程相关推荐