How to work with directories and files with spaces

S

s99999999s2003

hi

I am working in unix and i have some directories names with spaces
eg ABC DEF A
how can i work effectively with spaces in directory/file names in
python?

sometimes when i do os.path.join(dir_with_spaces,"-somestring" ) , it
gives me "-somestring" as the name only...without ABC DEF A
it should give me ABC DEF A-somestring as the directory name

thanks
 
M

Michele Petrazzo

hi

I am working in unix and i have some directories names with spaces
eg ABC DEF A
how can i work effectively with spaces in directory/file names in
python?

Like you can do with unix:

michele:~$ echo "Michele" > my\ name
michele:~$ python
Python 2.3.5 (#2, May 4 2005, 08:51:39)
[GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Michele
0
Michele
0

Bye,
Michele :)
 
L

Larry Bates

hi

I am working in unix and i have some directories names with spaces
eg ABC DEF A
how can i work effectively with spaces in directory/file names in
python?

sometimes when i do os.path.join(dir_with_spaces,"-somestring" ) , it
gives me "-somestring" as the name only...without ABC DEF A
it should give me ABC DEF A-somestring as the directory name

thanks

The spaces aren't the problem AND you are misreading what
os.path.join does:

join( path1[, path2[, ...]])

Joins one or more path components intelligently. If any component
is an absolute path, all previous components are thrown away, and
joining continues. The return value is the concatenation of path1,
and optionally path2, etc., with exactly one directory separator
(os.sep) inserted between components, unless path2 is empty. Note
that on windows, since there is a current directory for each drive,
os.path.join("c:", "foo") represents a path relative to the current
directory on drive C: (c:foo), not c:\\foo.


To do what you wanted (based on your example) I would do:

import os
fullpath="%s%s" % (dir_with_spaces, '-somestring')

-Larry Bates
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top