os.path.dirname adds unremoveable spaces?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason

    os.path.dirname adds unremoveable spaces?

    Here's the code I'm using:

    ############### ############### ######
    import os, string
    for root, dirs, files in os.walk('/home/_Comedy'):
    for file in files:
    str = os.path.dirname (file)
    print root, str.strip(), "/", file.strip()

    ############### ############### ######
    The problem is that even after using strip(), it still prints a list like
    this:


    /home/_Comedy/ISIRTA / ISIRTA - 1966.03.28 - s02e03 - Ali Baba.mp3
    /home/_Comedy/ISIRTA / ISIRTA - 1966.04.04 - s02e04 - Nelson.mp3
    /home/_Comedy/ISIRTA / ISIRTA - 1966.04.18 - s02e06 - Angus Prune.mp3
    ^^^^^
    ^^^^^
    I can't remove these mystery spaces that I'm pointing to no matter what I
    try. Neither the directories or filenames have spaces before or after
    them. Even if they did, they should've been removed when I used the strip
    command.

    Any ideas?
  • Jason

    #2
    Re: os.path.dirname adds unremoveable spaces?

    And just as expected, after an hour of searching google groups for an
    answer, I post the question only to figure it out 10 seconds later.
    Sheesh!

    I used os.path.join and all is well.




    Jason <asldfkjasldfj@ asldfkjasldkfja sdf.com> wrote in
    news:Xns94CE7B1 9D78E7nonenonec om@24.93.43.119 :
    [color=blue]
    > Here's the code I'm using:
    >
    > ############### ############### ######
    > import os, string
    > for root, dirs, files in os.walk('/home/_Comedy'):
    > for file in files:
    > str = os.path.dirname (file)
    > print root, str.strip(), "/", file.strip()
    >
    > ############### ############### ######
    > The problem is that even after using strip(), it still prints a list
    > like this:
    >
    >
    > /home/_Comedy/ISIRTA / ISIRTA - 1966.03.28 - s02e03 - Ali Baba.mp3
    > /home/_Comedy/ISIRTA / ISIRTA - 1966.04.04 - s02e04 - Nelson.mp3
    > /home/_Comedy/ISIRTA / ISIRTA - 1966.04.18 - s02e06 - Angus Prune.mp3
    >
    > ^^^^^
    > ^^^^^
    > I can't remove these mystery spaces that I'm pointing to no matter
    > what I try. Neither the directories or filenames have spaces before
    > or after them. Even if they did, they should've been removed when I
    > used the strip command.
    >
    > Any ideas?
    >[/color]

    Comment

    • Michael Geary

      #3
      Re: os.path.dirname adds unremoveable spaces?

      > > ############### ############### ######[color=blue][color=green]
      > > import os, string
      > > for root, dirs, files in os.walk('/home/_Comedy'):
      > > for file in files:
      > > str = os.path.dirname (file)
      > > print root, str.strip(), "/", file.strip()
      > >
      > > ############### ############### ######
      > > The problem is that even after using strip(), it still prints a list
      > > like this:
      > >
      > >
      > > /home/_Comedy/ISIRTA / ISIRTA - 1966.03.28 - s02e03 - Ali Baba.mp3
      > > /home/_Comedy/ISIRTA / ISIRTA - 1966.04.04 - s02e04 - Nelson.mp3
      > > /home/_Comedy/ISIRTA / ISIRTA - 1966.04.18 - s02e06 - Angus Prune.mp3
      > >
      > > ^^^^^
      > > ^^^^^
      > > I can't remove these mystery spaces that I'm pointing to no matter
      > > what I try. Neither the directories or filenames have spaces before
      > > or after them. Even if they did, they should've been removed when I
      > > used the strip command.[/color][/color]
      [color=blue]
      > And just as expected, after an hour of searching google groups for an
      > answer, I post the question only to figure it out 10 seconds later.
      > Sheesh!
      >
      > I used os.path.join and all is well.[/color]

      Just to add a couple of notes... You probably shouldn't be using the strip()
      function at all. What if the directory or filename does have leading or
      trailing spaces? Presumably you would want to keep those when constructing a
      full path.

      Also, you did figure out that it was the print statement adding the spaces,
      right? Try this test:

      print 'one', '/', 'two'

      and compare the results with this:

      print '%s/%s' %( 'one', 'two' )

      In the second example, you're giving a single argument to the print
      statement, a string that you've already formatted with the % operator.

      os.path.join is better for dealing with file paths, of course, but this will
      be useful for other things you might want to concatenate and format.

      -Mike


      Comment

      • Jason

        #4
        Re: os.path.dirname adds unremoveable spaces?

        That's some great info, thanks. I never actually figured out what was
        adding the spaces, so I appreciate your explanation. This is only my third
        day of Python.

        [color=blue]
        >
        > Just to add a couple of notes... You probably shouldn't be using the
        > strip() function at all. What if the directory or filename does have
        > leading or trailing spaces? Presumably you would want to keep those
        > when constructing a full path.
        >
        > Also, you did figure out that it was the print statement adding the
        > spaces, right? Try this test:
        >
        > print 'one', '/', 'two'
        >
        > and compare the results with this:
        >
        > print '%s/%s' %( 'one', 'two' )
        >
        > In the second example, you're giving a single argument to the print
        > statement, a string that you've already formatted with the % operator.
        >
        > os.path.join is better for dealing with file paths, of course, but
        > this will be useful for other things you might want to concatenate and
        > format.
        >
        > -Mike
        >
        >
        >[/color]

        Comment

        Working...