(回答)我想从这个目录中更改文件名。让我们称他们为ai01.aif,ab01.aif,anedab01.aif。
import os, sys
path="/Users/Stephane/Desktop/AudioFiles"
dirs=os.listdir(os.path.expanduser(path))
i="changed"
for file in dirs:
newname=i+file
os.rename(file,newname)我发现了一个错误:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'ai01.aif' -> 'changedai01.aif'
>>> 发布于 2016-01-04 21:10:20
当前目录中没有名为ai01.aif的文件(这通常是脚本所在的文件,但可能在其他目录中)。获得内容的目录不是当前目录。您需要将正在工作的目录添加到文件名的开头。
import os, sys
path = os.path.expanduser("/Users/Stephane/Desktop/AudioFiles")
dirs = os.listdir(path)
i = "changed"
for file in dirs:
newname = i + file
os.rename(os.path.join(path, file), os.path.join(path, newname))发布于 2016-01-04 21:11:35
我的error=>
我不在目录里。所以而不是
import os, sys
path="/Users/Stephane/Desktop/AudioFiles"
dirs=os.listdir(os.path.expanduser(path))
i="changed"
for file in dirs:
newname=i+file
os.rename(file,newname)它应该是:
import os, sys
path="/Users/Stephane/Desktop/AudioFiles"
dirs=os.listdir(os.path.expanduser(path))
i="changed"
for file in dirs:
newname=i+file我的错误就在下面
os.rename(path+"/"+file, path+"/"+newname)https://stackoverflow.com/questions/34599784
复制相似问题