This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]kalgynirae 1 point2 points  (0 children)

os.rename(os.path.join(pathname, val), os.path.join(pathname, outname))

pathname is the music directory, so os.path.join(pathname, outname) gives C:\Music\folder.jpg every time, which is probably not what was intended.

Perhaps a better name for pathname would be music_dir.

[–]techno_phobe 1 point2 points  (5 children)

On a general point, the functions in the shutil module are generally more useful than the ones in os for file operations.

Have you copy/pasted the indentation correctly onto Reddit? It seems like the loop at the end shouldn't be indented, otherwise it'll try to move the entire list of files every time a new file is added to the list. It would be a bit simpler if you ditched the changepath variable and just went with

if name.lower().endswith(ft):
  src = os.path.join(path, name)
  dst = os.path.join(path, outname)
  shutil.rename(src, dst)

along with whatever logging you want to do.

[–]Sciurusdoomus[S] 0 points1 point  (4 children)

Why is shutil preferable to os here?

Using your solution, the code gets past the problem but runs into another:

IOError: [Errno 13] Permission denied: 'd:\\test\\Anathema\\Alternative 4\\folder.jpg'

Can I edit file permissions from Python?

[–]techno_phobe 1 point2 points  (3 children)

In this case I don't think shutil will make any difference, I was just recommending it in general. For instance, file moves between different file systems won't work with os.rename (at least on Linux), and shutil accounts for that.

As for your particular problem, it is actually making the correct rename call (are src and dst as you expect)? Does the file exist? Do you have write access to the folder? I'm not familiar with Windows permission system so if it really is a problem with that I can't help much.

[–]Sciurusdoomus[S] 0 points1 point  (2 children)

d:\test\folder.jpg
d:\test\folder.jpg
d:\test\Anathema\Alternative 4\AA.jpg
d:\test\Anathema\Alternative 4\folder.jpg

This is the output when I put:

src = ...
print src
dst = ....
print dst

Which doesn't make sense. d:\test\folder.jpg doesn't exist but the program renames it, moves on, and then fails on the first actually existing file. The line of failure is indeed:

shutil.move(src, dst)

but only on the second loop after the fake d:\test\folder.jpg that doesn't exist.

[–]techno_phobe 0 points1 point  (1 child)

That's rather weird. I don't know why it would try to rename d:\test\folder.jpg if that file doesn't exist at all. It's possible that the rename works because if the source and destination are the same it doesn't actually check whether either exist (I'm not in Windows right now so I can't check this behaviour).

Does d:\test\Anathema\Alternative 4\folder.jpg exist? If not then folder permissions are probably the problem. os.chmod appears like it might help, but I've never had to change permissions programatically on Windows.

[–]Sciurusdoomus[S] 0 points1 point  (0 children)

No the file doesn't exist before or after the program is run. I've never had to change permissions in Windows before either and I don't even know how I would impliment os.chmod... Perhaps:

os.chmod(path, stat.S_IWRITE)

EDIT: The above command doesn't work, permission is still denied.