os.chdir doesn't accept variables sometimes

dannycolligan at gmail.com dannycolligan at gmail.com
Fri Jun 2 12:58:36 EDT 2006


I have a strange problem with os.chdir... here is my script that I am
using to edit the filenames of my music library:

#!/usr/bin/python

from os import *

chdir("/home/chainlynx/Desktop/Music")
for artist in listdir(getcwd()):
        print "===ARTIST: "+artist
        chdir(artist)
        for album in listdir(getcwd()):
                print "---ALBUM: "+album
                print "CWD: " + getcwd()
                chdir(album)                       ######ERROR ON THIS
LINE
                for string in listdir(album):
                        print "-SONG "+ string
                        if string[-3:] == "mp3":
                            print "CONVERTING "+string+" to
"+string[:string.index(".")]+".mp3"
#                            string = string[:string.index(".")]+".mp3"

The dummy file structure that I set up to run this:

chainlynx at cronus:~/Desktop/Music$ find .
.
./AAAAA
./AAAAA/Album1
./AAAAA/Album2
./AAAAA/Album3
./AAAAA/Album3/song1.m4a.x.mp3
./BBBBB
./CCCCC
./CCCCC/Albummmmm
./CCCCC/Albummmmm/asdfasdf.ogg
./CCCCC/Albummmmm/blah.m4a.wav.mp3
./CCCCC/Albummmmm/good.mp3

The error I get:

chainlynx at cronus:~/workspace/PyTest/src/pypack$ python __init__.py
===ARTIST: AAAAA
---ALBUM: Album1
CWD: /home/chainlynx/Desktop/Music/AAAAA
Traceback (most recent call last):
  File "/home/chainlynx/workspace/PyTest/src/pypack/__init__.py", line
12, in ?
    for string in listdir(album):
OSError: [Errno 2] No such file or directory: 'Album1'

Does anyone know why this is choking?  Clearly, because of the second
chdir(), chdir() can accept variables like this... what am I doing
wrong?

Thanks in advance,

Danny

P.S. Bonus points: is there any way to bash shell script this on the
command line instead (recursively)?




More information about the Python-list mailing list