OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

os.rename() fails with WinError 3: The system cannot find the path specified on Windows but works on Linux

  • Thread starter Thread starter Mayank Gupta
  • Start date Start date
M

Mayank Gupta

Guest
I am using the following code to semi-automate renaming of media files

Code:
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:

Code:
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'

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-

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'
<p>I am using the following code to semi-automate renaming of media files</p>
<pre><code>

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
</code></pre>
<p>this given code works on debian/ubuntu but seems to fail while being run on windows. It outputs the following error code:</p>
<pre><code>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'
</code></pre>
<p>While this works perfectly on debian/ubuntu it gives this error on Windows.</p>
<p>I tried looking through solutions on stack and came across using os.renames instead of os.rename, but that outputs the same error code-</p>
<pre><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'
</code></pre>
 

Latest posts

Top