Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to create file with spaces

Reply
Thread Tools

how to create file with spaces

 
 
s99999999s2003@yahoo.com
Guest
Posts: n/a
 
      04-06-2006
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.

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-06-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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>



 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-06-2006
> 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>



 
Reply With Quote
 
Fulvio
Guest
Posts: n/a
 
      04-06-2006
Alle 18:18, giovedý 06 aprile 2006, (E-Mail 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
 
Reply With Quote
 
Larry Bates
Guest
Posts: n/a
 
      04-06-2006
Fulvio wrote:
> Alle 18:18, giovedý 06 aprile 2006, (E-Mail 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


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

-Larry Bates
 
Reply With Quote
 
infidel
Guest
Posts: n/a
 
      04-06-2006
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('')

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
[CSS] how can I show spaces as spaces? Tomasz Chmielewski HTML 21 09-10-2009 06:43 PM
how can I show spaces as spaces, part 2 Tomasz Chmielewski HTML 14 09-10-2009 03:54 PM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM



Advertisments