我使用以下代码来半自动重命名媒体文件
def findingmedia(directory):
media=[]
for path,subdir, files in os.walk(directory):
for file in files:
#Appends the path and names of video files into media list.
if file.lower().endswith(video_formats):
media.append((path,file))
return media
#Rename according to inputs provided by the user
def rename(media):
count=0
for element in media:
#Take input for the new name from user
print(f'The name of this file is {element[1]}')
nname=str(input("Enter the new name for this file \n"))
splitname=element[1].split('.')
format=splitname[-1]
nname=nname+'.'+format
try:
file=os.path.join(element[0], element[1])
newname= os.path.join(element[0], nname)
#Rename
os.rename(file, newname)
print(f'File with name {element[1]} renamed to {nname} successfully')
count+=1
except OSError as error:
print(f"Could not rename file {element[1]} \n Error = {error}")
return count
这个给定的代码可以在 debian/ubuntu 上运行,但在 Windows 上运行时似乎会失败。它输出以下错误代码:
Could not rename file mediatest1.mkvError = [WinError 3] The system cannot find the path specified: 'C:\Users\mg15d\Downloads\The mediatest1 1080p AMZN WEB-DL DDP5 1 H 264-NTb[TGx]\mediatest1.1080p.AMZN.WEB-DL.DDP5.1.H.264-NTb.mkv' -> 'C:\Users\mg15d\Downloads\mediatest1 1080p AMZN WEB-DL DDP5 1 H 264-NTb[TGx]\renamedwuhu.mkv'
虽然这在 debian/ubuntu 上完美运行,但在 Windows 上却出现此错误。
我尝试查看堆栈上的解决方案,发现使用 os.renames 而不是 os.rename,但输出相同的错误代码 -
Error = [WinError 3] The system cannot find the path specified: 'C:\\Users\\mg15d\\Downloads\\The mediatest1 1080p AMZN WEB-DL DDP5 1 H 264-NTb[TGx]\\mediatest1.1080p.AMZN.WEB-DL.DDP5.1.H.264-NTb.mkv' -> 'C:\\Users\\mg15d\\Downloads\\mediatest1 1080p AMZN WEB-DL DDP5 1 H 264-NTb[TGx]\\renamedwuhu.mkv'
Windows 很奇怪,更改目录名称本身,然后使用新目录名称重新启动程序就可以了。