SciurusDoomus
SciurusDoomus

Reputation: 113

"WindowsError 183" on os.rename()

#Opens a directory and outputs a text file there that lists every subdirectory in it

import os
from shutil import move

pathname = raw_input('Enter path for music directory (ex. C:\\Music): ')
fn = raw_input('Enter desired file name for all converted files: ')
ft = raw_input('Enter the file extension you want the program to look for (ex. .jpg): ')
changepath = []
os.chdir(pathname)
for path, subdirs, files in os.walk(pathname):
        for name in files:
            changepath.append(os.path.join(path, name))
for idx, val in enumerate(changepath):
    if val.lower().endswith(ft):
        os.rename(val, (fn + ft))
print('Complete')

I'm using this to rename all the album artwork in my music folder to one thing, something like new.jpg.

This code fails on line 16 "os.rename(val, (fn + ft))" with error 183. When I use "os.rename(val, val + (fn + ft))" it works but it calls the file something like old.jpgnew.jpg instead of new.jpg which is what I want.

When the code fails (written as it is in the block above), I get a new.jpg file in the music directory. It's the renamed album artwork of the first subdirectory, but no album artwork files after the first are renamed. It fails after successfully renaming the first image, but for some reason moves it out of its original directory and into the parent "Music" directory.

Upvotes: 0

Views: 2041

Answers (1)

cwgem
cwgem

Reputation: 2799

Updated Answer

Okay I didn't see the part where a different method was applied. This:

os.rename(val, (fn + ft))

to:

os.rename(val, (os.path.dirname(val) + fn + ft))

is probably what you want

Old Answer

Error 183 in Windows is for an already existent file. Looking over your code a bit:

fn = raw_input('Enter desired file name for all converted files: ')
ft = raw_input('Enter the file extension you want the program to look for (ex. .jpg): ')

There's no apparent path info here so:

for idx, val in enumerate(changepath):
    if val.lower().endswith(ft):
        os.rename(val, (fn + ft))

Where is the path for fn + ft? Based on your logic it would just end up in wherever you originally chdir'ed to. Which given that fn and ft appear to be static values that would be why you'd get the file already exists.

Upvotes: 4

Related Questions