how to create file with spaces

Discussion in 'Python' started by s99999999s2003@yahoo.com, Apr 6, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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>
     
    Fredrik Lundh, Apr 6, 2006
    #2
    1. Advertising

  3. > 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>
     
    Fredrik Lundh, Apr 6, 2006
    #3
  4. Fulvio Guest

    Alle 18:18, giovedì 06 aprile 2006, 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
     
    Fulvio, Apr 6, 2006
    #4
  5. Larry Bates Guest

    Fulvio wrote:
    > Alle 18:18, giovedì 06 aprile 2006, 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
     
    Larry Bates, Apr 6, 2006
    #5
  6. infidel Guest

    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('')
     
    infidel, Apr 6, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John B. Matthews
    Replies:
    4
    Views:
    670
    John B. Matthews
    Sep 12, 2008
  2. Roedy Green
    Replies:
    3
    Views:
    626
  3. Tomasz Chmielewski

    [CSS] how can I show spaces as spaces?

    Tomasz Chmielewski, Sep 5, 2009, in forum: HTML
    Replies:
    21
    Views:
    1,604
    Jan C. Faerber
    Sep 10, 2009
  4. Tomasz Chmielewski

    how can I show spaces as spaces, part 2

    Tomasz Chmielewski, Sep 9, 2009, in forum: HTML
    Replies:
    14
    Views:
    877
    Jan C. Faerber
    Sep 10, 2009
  5. johkar
    Replies:
    2
    Views:
    2,902
    Mayeul
    Dec 10, 2009
Loading...

Share This Page