目前我的文件夹中有38个子文件夹测试子文件夹名称从 01 开始到 38 。每个子文件夹有2个wav文件,这些文件是随机命名的,我想正确地按顺序重命名它。 例如: 子文件夹 01 有wav文件我的录音#1 和我的录音#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
答案 0 :(得分:1)
你有两个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
答案 1 :(得分:0)
你有内部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)