os.chdir doesn't accept variables sometimes

D

dannycolligan

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@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@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)?
 
B

BartlebyScrivener

for album in listdir(getcwd()):

doesn't listdir give you subdirectories AND files?

So, then if you try to: chdir(album)

If album is a file, it chokes?

Just a guess. I'm on Windows, not Linux.

rd
 
D

Donn Cave

#!/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): ....

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'

To start with, note that your traceback implicates the listdir()
on line 12, not the chdir() before it. This listdir() uses the
same parameter as that preceding chdir(), that appears to be your
problem.

One of your problems, anyway. You're doing a lot of downwards
chdirs, but no upwards, which is going to limit the extent of
your directory traversal.

The "from os import *" is a terrible idea, where did you get that?
"os" has a lot of identifiers in it that tend to collide with other
namespaces. "open" is a classic example. Don't do that, with "os"
or generally any module.

As a more general direction, it would be a good idea to look
into standard library functions, e.g., os.path.walk
P.S. Bonus points: is there any way to bash shell script this on the
command line instead (recursively)?

Depends on what you want it to do, but maybe something like

find . -name \*.mp3 -exec $HOME/bin/cvt .mp4 {} \;

where cvt would be something like
#!/bin/sh
case $1:$2 in
.mp4:*.mp3) mp3_to_mp4 $2 ${2%.mp3}.mp4 ;;
...

You'd have to think about it.

Donn Cave, (e-mail address removed)
 
D

DataSmash

A simple way to get all the files throughout the directory sturcture...
You may have to rewrite the "CONVERTING" part.

import os, glob

for root, dirs, files in os.walk(os.getcwd()):
for file in files:
if file.endswith(".mp3"):
print "File: " + os.path.abspath(os.path.join(root, file))
print "CONVERTING "+file+" to
"+file[:file.index(".")]+".mp3"
file = file[:file.index(".")]+".mp3"


Donn said:
#!/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): ...

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'

To start with, note that your traceback implicates the listdir()
on line 12, not the chdir() before it. This listdir() uses the
same parameter as that preceding chdir(), that appears to be your
problem.

One of your problems, anyway. You're doing a lot of downwards
chdirs, but no upwards, which is going to limit the extent of
your directory traversal.

The "from os import *" is a terrible idea, where did you get that?
"os" has a lot of identifiers in it that tend to collide with other
namespaces. "open" is a classic example. Don't do that, with "os"
or generally any module.

As a more general direction, it would be a good idea to look
into standard library functions, e.g., os.path.walk
P.S. Bonus points: is there any way to bash shell script this on the
command line instead (recursively)?

Depends on what you want it to do, but maybe something like

find . -name \*.mp3 -exec $HOME/bin/cvt .mp4 {} \;

where cvt would be something like
#!/bin/sh
case $1:$2 in
.mp4:*.mp3) mp3_to_mp4 $2 ${2%.mp3}.mp4 ;;
...

You'd have to think about it.

Donn Cave, (e-mail address removed)
 

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top