473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

os.chdir doesn't accept variables sometimes

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

Jun 2 '06 #1
3 5061
>> 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

Jun 2 '06 #2
In article <11*********************@u72g2000cwu.googlegroups. com>,
"da***********@gmail.com" <da***********@gmail.com> wrote:
#!/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, do**@u.washington.edu
Jun 2 '06 #3
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 Cave wrote:
In article <11*********************@u72g2000cwu.googlegroups. com>,
"da***********@gmail.com" <da***********@gmail.com> wrote:
#!/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, do**@u.washington.edu


Jun 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: HaAwK | last post by:
here is the code, basically: ---------------------- Dim HTMLString As String Dim WebClient As New System.Net.WebClient() Dim querystring As New...
1
by: Ingo Nolden | last post by:
Hi, I am using spirit 1.31 I have been trying the following example from the spirit docs. I tried it with int and double neither works: vector<int> v; rule<> r = list_p(int_p, ch_p(',')); ...
2
by: Robert V. Hanson | last post by:
If you have per user information that you want to store in session and the setting in web.config is set to cookieless=false so you are intending on using cookies, the user's browser is set to not...
3
by: Dan Holmes | last post by:
The first tim ethis app is run it works just fine as well as every restart. What i can't figure is how to get it to accept a connection after the first one. This is a console app but i don't...
2
by: jimmy | last post by:
Does someone know why BackGroundWorker.ReportProgress method doesn't accept strings ? You can't pass integer always for a progress meter as percentage completed cannot be calculated in many...
17
by: Michael Reichenbach | last post by:
Here is the example code. int main(int argc, char *argv) { string Result; WIN32_FIND_DATA daten; HANDLE h = FindFirstFile(TEXT("c://test"), &daten); system("PAUSE"); return EXIT_SUCCESS; }
1
by: lbelkova | last post by:
Hello, I've created a custom DataGridViewColumn. Everything work well, except for some reason the column doesn't accept some of the chars: "q", "." and "'". Did anybody have a similar problem?...
3
by: YSpa | last post by:
Hi, I'm using SQL-Server Express 2005 on Windows XP Prof. and after working properly for some time my asp.net application suddenly gave the error that my DateFormat wasn't accepted while using...
2
oll3i
by: oll3i | last post by:
function myClass(modul,var1,var2,var3,var4,var5,var6,var7,var8,domainURL){ this.modul=modul; this.var1=var1; this.var2=var2; this.var3=var3; this.var4=var4; this.var5=var5; this.var6=var6;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.