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? 3 3237
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***********@ asldfkjasldkfja sdf.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?
> > ############### ############### ###### 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
Directory.CreateDirectory(dirName)
Label1.Text = "Directory created"
Else
|
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 Python
standard library, and Reinhold Birkenfeld started a discussion on it in
python-dev
<http://mail.python.org/pipermail/python-dev/2005-June/054438.html>.
The upshot of the discussion was that many python-dev'ers wanted path
added to the...
|
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 following but for some reason it doesnt split the string at
all. It just returns the full string.
|
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 using '/' as delimiters
and get the last 'word'. The reason I'm not entirely happy with that
method is that it is platform specific. I would prefer to use a built
in method if possible.
Cheers,
|
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 pathname component and head is everything leading up to that. The
tail part will never contain a slash; if path ends in a slash, tail
will be empty. If there is no slash in path, head will be empty. If
| |
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
part and a extension part. For example:
'/Users/t/web/perl-python/I_Love_You.html'
becomes
|
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 server. On the line: dirs = Directory.GetDirectories(currentDir) I get "Access to the path "\\les-net\les\Special Projects\ATSPDF" is denied." How do I get the GetDirectories command to user my user ID and password when it tries to hit the...
|
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
print os.path.join(os.path.dirname(os.tmpnam()),*("a","b","c"))
#works
OUTPUT:/var/tmp/a/b/c
and
|
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, realpath, abspath
here = dirname(realpath(abspath(__file__.rstrip("c"))))
In particular, this takes care of the case of symlinked, compiled
scripts, which is fairly common at my workplace, and I imagine in many
*nix evironments.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |