471,054 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,054 software developers and data experts.

how to touch a file

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.

Apr 6 '06 #1
1 1674
s9************@yahoo.com wrote:
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?


os.path.join joins paths with the proper OS-dependend separator. So

os.path.join("a", "b", "c")

becomes

a/b/c

under unix. Naturally, joining dir and "-dummy" produces "<dir>/-dummy" -
which is what you see.

Instead, do something like this:

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

Apr 6 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by pekka niiranen | last post: by
2 posts views Thread by Robert May | last post: by
10 posts views Thread by Kenneth McDonald | last post: by
25 posts views Thread by Michelle | last post: by
1 post views Thread by =?Utf-8?B?dmNs?= | last post: by
2 posts views Thread by Chuck Anderson | last post: by

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.