how to create file with spaces

S

s99999999s2003

hi

i have a dir that contains directories with names and spaces in between
example

rootdir
| ----> ABC DEF A
| ---> BDD SD N

I wanted to touch a file with the same name as the directories inside
each directory

rootdir
| ----> ABC DEF A
|-------> ABC DEF A-dummy
| ---> BDD SD N
|-------> BDD SD N-dummy

heres the code :
for d in os.walk(rootdir):
(dirpath, dirnames, filenames) = d
for dir in [dirpath]:
if not os.path.exists( os.path.join(dir,"-dummy") ):
f = open( os.path.join(dir,"-dummy") ,
"w").write("")

but i got only "-dummy" as the filename in each directory

How can i deal with spaces in this case? or is there some wrong things
i do in the code?
thanks for any help.
 
F

Fredrik Lundh

I wanted to touch a file with the same name as the directories inside
each directory

rootdir
| ----> ABC DEF A
|-------> ABC DEF A-dummy
| ---> BDD SD N
|-------> BDD SD N-dummy

heres the code :
for d in os.walk(rootdir):
(dirpath, dirnames, filenames) = d
for dir in [dirpath]:
if not os.path.exists( os.path.join(dir,"-dummy") ):
f = open( os.path.join(dir,"-dummy") ,
"w").write("")

but i got only "-dummy" as the filename in each directory

os.path.join joins path elements, so that's entirely expected.

try

filename = dirpath + "-dummy"
if not os.path.isfile(filename):
open(filename, "w").close()

instead. or if you want to "touch" the file even if it already exists,
just do

open(dirpath + "-dummy", "w").close()

(to only touch a file if it exists, you can use os.utime(filename, None)
instead)

</F>
 
F

Fredrik Lundh

try
filename = dirpath + "-dummy"
if not os.path.isfile(filename):
open(filename, "w").close()

better make that

basename = os.path.basename(dirpath) + "-dummy"
filename = os.path.join(dirpath, basename)
if not os.path.isfile(filename):
open(filename, "w").close()

</F>
 
F

Fulvio

Alle 18:18, giovedì 06 aprile 2006, (e-mail address removed) ha scritto:
How can i deal with spaces in this case?
I don't have an idea with python, but if can help I may say that bash you
might use "\ " to escape a space or use a quoted full path.
The shell program "basename" is failing, anyhow with file names containing
spaces.

F
 
L

Larry Bates

Fulvio said:
Alle 18:18, giovedì 06 aprile 2006, (e-mail address removed) ha scritto:
I don't have an idea with python, but if can help I may say that bash you
might use "\ " to escape a space or use a quoted full path.
The shell program "basename" is failing, anyhow with file names containing
spaces.

F

Python works fine with directories AND files with spaces in them.
Your problems are somewhere else.

-Larry Bates
 
I

infidel

dirpath is just a string, so there's no sense in putting it in a list
and then iterating over that list.

If you're trying to do something with each file in the tree:

for dir, subdirs, names in os.walk(rootdir):
for name in names:
filepath = os.path.join(dir, name) + "-dummy"
if not os.path.exists(filepath):
f = open(filepath, 'w').write('')
 

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,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top