os.chdir doesn't accept variables sometimes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dannycolligan@gmail.com

    #1

    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@cronu s:~/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.mp 3
    ../CCCCC/Albummmmm/good.mp3

    The error I get:

    chainlynx@cronu s:~/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)?

  • BartlebyScrivener

    #2
    Re: os.chdir doesn't accept variables sometimes

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

    Comment

    • Donn Cave

      #3
      Re: os.chdir doesn't accept variables sometimes

      In article <1149267516.348 440.60290@u72g2 000cwu.googlegr oups.com>,
      "dannycolligan@ gmail.com" <dannycolligan@ gmail.com> wrote:
      [color=blue]
      > #!/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):[/color]
      ....
      [color=blue]
      > 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'[/color]

      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
      [color=blue]
      > P.S. Bonus points: is there any way to bash shell script this on the
      > command line instead (recursively)?[/color]

      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, donn@u.washingt on.edu

      Comment

      • DataSmash

        #4
        Re: os.chdir doesn't accept variables sometimes

        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.getc wd()):
        for file in files:
        if file.endswith(" .mp3"):
        print "File: " + os.path.abspath (os.path.join(r oot, file))
        print "CONVERTING "+file+" to
        "+file[:file.index("." )]+".mp3"
        file = file[:file.index("." )]+".mp3"


        Donn Cave wrote:[color=blue]
        > In article <1149267516.348 440.60290@u72g2 000cwu.googlegr oups.com>,
        > "dannycolligan@ gmail.com" <dannycolligan@ gmail.com> wrote:
        >[color=green]
        > > #!/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):[/color]
        > ...
        >[color=green]
        > > 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'[/color]
        >
        > 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
        >[color=green]
        > > P.S. Bonus points: is there any way to bash shell script this on the
        > > command line instead (recursively)?[/color]
        >
        > 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, donn@u.washingt on.edu[/color]

        Comment

        Working...