[英]os.rename() not working in my python script
我正在編寫一個腳本,將目錄“ ./itunes和music / F14 /”中的所有.mp3,.m4a和.m4p文件更改為另一個標題。 我可以獲取文件名,並且使用hsaudiotag可以獲取標題標簽。 但是,當我嘗試將文件重命名為title標簽時,會出現以下錯誤:
WindowsError: [Error 2] The system cannot find the file specified
這是我的代碼:
from hsaudiotag import auto
import os
def main():
for filename in os.listdir('./itunes and music/F14/'):
print(filename)
os.rename(filename, filename[2:])
myfile = auto.File('./itunes and music/F14/'+filename)
print(myfile.title)
if filename.endswith(".mp3"):
print('3')
os.rename(filename, myfile.title+".mp3")
elif filename.endswith(".m4a"):
print('4a')
os.rename(filename, myfile.title+".m4a")
elif filename.endswith(".m4p"):
print('4p')
os.rename(filename, myfile.title+".m4p")
main()
所有的打印語句僅用於調試,並且都可以正常工作。 只是os.rename()函數不是。
指定文件路徑,而不僅僅是文件名。
from hsaudiotag import auto
import os
def main():
d = './itunes and music/F14/'
for filename in os.listdir(d):
print(filename)
filepath = os.path.join(d, filename)
os.rename(filepath, filepath[2:])
myfile = auto.File(filepath)
print(myfile.title)
if filename.endswith(".mp3"):
print('3')
os.rename(filepath, os.path.join(d, myfile.title+".mp3"))
elif filename.endswith(".m4a"):
print('4a')
os.rename(filepath, os.path.join(d, myfile.title+".m4a"))
elif filename.endswith(".m4p"):
print('4p')
os.rename(filepath, os.path.join(d, myfile.title+".m4p"))
main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.