I am using the following code to semi-automate renaming of media files
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
this given code works on debian/ubuntu but seems to fail while being run on windows. It outputs the following error code:
Could not rename file mediatest1.mkvError = [WinError 3] The system cannot find the path specified: 'C:Usersmg15dDownloadsThe mediatest1 1080p AMZN WEB-DL DDP5 1 H 264-NTb[TGx]mediatest1.1080p.AMZN.WEB-DL.DDP5.1.H.264-NTb.mkv' -> 'C:Usersmg15dDownloadsmediatest1 1080p AMZN WEB-DL DDP5 1 H 264-NTb[TGx]renamedwuhu.mkv'
While this works perfectly on debian/ubuntu it gives this error on Windows.
I tried looking through solutions on stack and came across using os.renames instead of os.rename, but that outputs the same error code-
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'