重命名文件为列表中的条目。

huangapple go评论67阅读模式
英文:

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_aVP02变成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')

huangapple
  • 本文由 发表于 2020年1月6日 21:36:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定