英文:
Rename files after entries in a list
问题
I can help you with the translation. Here is the translated text:
我需要按系统方式重命名一些文件。因此,我有一个希望将文件名重命名的名称列表。文件夹中包含命名为wav
的文件,命名如下:
VP01.wav
VP02.wav
VP03.wav
ID_list
中的顺序已经是正确的顺序。所以基本上我想让VP01
变成01_a
,VP02
变成03_a
,依此类推。我尝试这样做:
ID_list = ['01_a', '03_a', '04_b', '01_b', '05_a', '04_a', '03_b']
import os
path = glob.glob('filepath\*.wav')
for item in path, ID_list:
os.rename(item, item)
但它给我报错:
TypeError: rename: src should be string, bytes or os.PathLike, not list
我想要更改的文件是wav
文件。
有人知道如何做吗?
英文:
I need to rename a few files systematically. So I have a list of names I want the file names to rename in. The folder consists of wav
files, that are named like:
VP01.wav
VP02.wav
VP03.wav
The order in the ID_list
is already the right order. So basically I want that VP01
will be 01_a
, VP02
will be 03_a
, etc. I tried to do it like this:
ID_list = ['01_a', '03_a', '04_b', '01_b', '05_a', '04_a', '03_b']
import os
path = glob.glob('filepath\*.wav')
for item in path, ID_list:
os.rename(item, item)
But it gives me:
TypeError: rename: src should be string, bytes or os.PathLike, not list
as error. The files I want to change are wav
files.
Does someone know how to do it?
答案1
得分: 1
以下是翻译好的内容:
from pathlib import Path
file_path = Path("包含wav文件的路径/")
path = file_path.glob('**/*.wav')
for en, x in enumerate(path):
x.rename(ID_list + '.wav')
英文:
Here's a way to do:
from pathlib import Path
file_path = Path("path_containing_wav_files/")
path = file_path.glob('**/*.wav')
for en, x in enumerate(path):
x.rename(ID_list + '.wav')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论