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

乔曼

目前我的文件夹test 中有 38个子文件夹子文件夹名称从0138每个子文件夹有 2 个随机命名的 wav 文件,我想正确地按顺序重命名它。例如:子文件夹01有 wav 文件我的录音 #1我的录音 #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
小D

您有内部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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用zmv重命名子文件夹中的文件

python:通过子文件夹名称重命名子文件夹中的文件

使用父文件夹的名称重命名子文件夹中的文件

重命名文件夹中的所有文件也重命名子文件夹

移动或重命名子文件夹

重命名子文件夹中的文件并上传

重命名子文件夹中的文件

重命名子文件夹中的PowerShell错误

使用python和os.walk()重命名子文件夹中的文件

使用完整目录路径重命名子文件夹中的特定文件

如何使用 python 和 pandas 重命名子文件夹中的多个 .Json 文件

重命名子文件夹的特定级别

如何重命名子文件夹或zip文件中的文本文件

重命名文件夹中的文件

重命名文件夹中的文件

重命名不同子文件夹中的文件

我可以使用Powershell基于.CSV文件重命名子文件夹中的文件吗

重命名子文件夹:从父文件夹添加前导词

如何仅重命名子文件夹而不更改父文件夹名称

在文件夹,子文件夹和多个文件中递归查找和重命名

批。批量重命名文件夹和所有子文件夹中的文件

用“查找”重命名子文件夹中的所有.txt文件

重命名子文件夹中的文件并通过批处理覆盖

重命名子目录中与文件夹名称相同的文件

使用Matlab重命名文件夹中的文件

遍历文件夹并使用文件夹名称顺序重命名每个文件夹中的所有文件

在Python中重命名文件夹

使用 VBA 重命名文件夹

Powershell文件和子文件夹文件重命名