473,326 Members | 2,023 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,326 software developers and data experts.

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?
Jul 18 '05 #1
3 3208
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 <as***********@asldfkjasldkfjasdf.com> wrote in
news:Xn*************************@24.93.43.119:
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?


Jul 18 '05 #2
> > ####################################
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.
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.


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
Jul 18 '05 #3
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.


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


Jul 18 '05 #4

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

Similar topics

1
by: Matthias Ludwig | last post by:
I'm trying to create a directory on the web server with a vb.net code: .... Dim dirName As String = "w:\filepath\images" If Not Directory.Exists(dirName) Then...
70
by: Michael Hoffman | last post by:
Many of you are familiar with Jason Orendorff's path module <http://www.jorendorff.com/articles/python/path/>, which is frequently recommended here on c.l.p. I submitted an RFE to add it to the...
5
by: SimonH | last post by:
Hi all, I'm having problems identifying how to get the last directoy in a path string. For example, in the Path 'C:\dir1\dir2\dir3' I want to be able to get the name dir3 I've tried the...
2
by: Rob Cowie | last post by:
Hi, Given a string representing the path to a file, what is the best way to get at the filename? Does the OS module provide a function to parse the path? or is it acceptable to split the string...
12
by: Xah Lee | last post by:
Python Doc Problem Example Quote from: http://docs.python.org/lib/module-os.path.html ---------- split( path) Split the pathname path into a pair, (head, tail) where tail is the last...
3
by: Xah Lee | last post by:
Split File Fullpath Into Parts Xah Lee, 20051016 Often, we are given a file fullpath and we need to split it into the directory name and file name. The file name is often split into a core...
2
by: Tom Wells | last post by:
I have a little file upload page that I have been able to use to successfully upload files to the C: drive of LocalHost (my machine). I need to be able to upload to a network drive from the intranet...
1
by: Steve | last post by:
I have been trying to find documentation on the behavior Can anyone tell me why the first example works and the second doesn't and where I can read about it in the language reference? Steve ...
6
by: bukzor | last post by:
I have to go into these convulsions to get the directory that the script is in whenever I need to use relative paths. I was wondering if you guys have a better way: from os.path import dirname,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.