Cannot create a new file

Discussion in 'Python' started by Eric Belanger, Jun 22, 2004.

  1. Hi,

    Before posting I made sure I upgraded from 2.3.3 to 2.3.4, but the
    problem is still here.

    Ive created a little script which, at the end of it, deals with copying
    two files. All the script works fine except when its at the copy
    line(s). Nothing gets copied, it crashes at the first open() line.

    Before using open() I made it the lazy way first and wrote system("cp
    "+file1+" "+file2), but gaves me that message:

    IOError: [Errno 2] No such file or directory:
    '/home/bilange/.hnb-backup/1087861694'

    I then thought python didnt like file handling external commands, so I
    tried open()'ing and dealing with read() and write(), without any more
    luck (same error message).

    After two hours of blindly trying to debug this i finally discovered
    shutils (how to easily waste two hours by trying to reinvent the wheel
    :/), which ALSO gives me that error message I just pasted.

    Now I searched on the net for it, Im aware that sometimes for an obscure
    reason (i think its on mod_python though) the last character from the
    complete path may be omitted, which is not the case for me, I also tried
    to str() the path (advice taken from some GUI python tutorial on the net
    - theres no GUI in my case, but its a last resort attempt :) )

    Heres some info you might need to help me debug this:

    sys.version: 2.3.4 (#1, Jun 21 2004, 19:20:19) [GCC 3.3.3 20040412 (Red
    Hat Linux 3.3.3-7)] (Thats Fedora Core 2, by the way)

    the folder have "rwxrwxr-x" permission, and no file is present in the
    directory. I tried to copy manually (from bash) the file and of course,
    no problems here. The script is obviously run from the user bilange; im
    editing and running the code from the same user.

    If you need any more info, tell me.

    Thanks in advance!

    Eric Belanger
    PS: The E-mail address is fake, but easy to figure out. I got enough
    spam in my mailbox, thank you. :)
    Eric Belanger, Jun 22, 2004
    #1
    1. Advertising

  2. Eric Belanger

    Peter Hansen Guest

    Eric Belanger wrote:

    > Ive created a little script which, at the end of it, deals with copying
    > two files. All the script works fine except when its at the copy
    > line(s). Nothing gets copied, it crashes at the first open() line.
    >
    > Before using open() I made it the lazy way first and wrote system("cp
    > "+file1+" "+file2), but gaves me that message:
    >
    > IOError: [Errno 2] No such file or directory:
    > '/home/bilange/.hnb-backup/1087861694'


    Could you please include the full traceback, cut and pasted from
    the console, so that we can see the parts just before what you
    show above?

    Also it would probably be a good idea to include a code snippet
    showing the previous ten-or-so commands just before the failing
    line.

    -Peter
    Peter Hansen, Jun 22, 2004
    #2
    1. Advertising

  3. Eric Belanger

    John Guest

    Peter Hansen wrote:

    > Eric Belanger wrote:
    >
    >> Ive created a little script which, at the end of it, deals with
    >> copying two files. All the script works fine except when its at the
    >> copy line(s). Nothing gets copied, it crashes at the first open() line.
    >>
    >> Before using open() I made it the lazy way first and wrote system("cp
    >> "+file1+" "+file2), but gaves me that message:
    >>
    >> IOError: [Errno 2] No such file or directory:
    >> '/home/bilange/.hnb-backup/1087861694'

    >
    >
    > Could you please include the full traceback, cut and pasted from
    > the console, so that we can see the parts just before what you
    > show above?
    >
    > Also it would probably be a good idea to include a code snippet
    > showing the previous ten-or-so commands just before the failing
    > line.
    >
    > -Peter


    Use copyfile from shutil module instead. More Pythonic and you can debug
    better. I think you are not in the right directory. Including abit more
    code in the posting helps.
    John, Jun 22, 2004
    #3
  4. > Could you please include the full traceback, cut and pasted from
    > the console, so that we can see the parts just before what you
    > show above?
    >
    > Also it would probably be a good idea to include a code snippet
    > showing the previous ten-or-so commands just before the failing
    > line.


    Yes, my bad. I knew I missed something.

    Traceback (most recent call last):
    File "./hnbupdate", line 42, in ?
    localtousb()
    File "./hnbupdate", line 12, in localtousb

    shutil.copyfile("/home/bilange/usb/.hnb","/home/bilange/.hnb-backup/"+str(int(time.time())))
    File "/usr/local/lib/python2.3/shutil.py", line 38, in copyfile
    fdst = open(dst, 'wb')
    IOError: [Errno 2] No such file or directory:
    '/home/bilange/.hnb-backup/1087869914'

    ---

    #Note, I removed the unnessary parts. Heres what you need to replicate
    the bug. Notes are commented out to do a quick "copy-paste-execute"

    import os.path, sys
    from stat import *
    import datetime,time,os
    import shutil

    #identations are screwed up in thunderbird for those defs, lets hope it
    wont appear online.
    #In short, i wanna copy the current most recent .hnb file to
    ..hnb-backup/timestamp. str(int()) are needed to remove the milliseconds,
    which i first thought was the source of the problem (who knows)
    def usbtolocal():

    shutil.copyfile("/home/bilange/.hnb","/home/bilange/.hnb-backup/"+str(int(time.time())))
    shutil.copyfile("/home/bilange/usb/.hnb","/home/bilange/.hnb")


    def localtousb():

    shutil.copyfile("/home/bilange/usb/.hnb","/home/bilange/.hnb-backup/"+str(int(time.time())))
    shutil.copyfile("/home/bilange/usb/.hnb","/home/bilange/.hnb")

    #
    #Lots of code removed, was putting the values of os.stat(.hnb)[ST_MTIME]
    into terrier and usb variables. In short, it determines whether I have
    to copy from or to my USB drive.

    main_choice = raw_input('Proceed with sync? [O/n] ')
    main_choice = main_choice[0:1]
    if main_choice.lower() == "o":
    print "OK."
    if terrier < usb:
    usbtolocal()
    elif usb < terrier:
    localtousb()
    elif main_choice.lower() == "n":
    print "Cancelled"
    sys.exit(0)
    Eric Belanger, Jun 22, 2004
    #4
  5. Eric Belanger

    Bryan Guest

    > #identations are screwed up in thunderbird for those defs, lets hope it
    > wont appear online.



    i use thunderbird and i don't have indenting problems when posting python code to a newsgroup such as this one. when i
    post python code on a mailing list, it seems the format cannot be set to "text only". you have to change it to rich
    text. if i didn't just make a fool of myself :) the following indentation should look fine.


    def foo():
    print 'this is indented 4 spaces'
    for y in range(10):
    print '''this is indented 8 spaces'



    bryan
    Bryan, Jun 22, 2004
    #5
  6. Eric Belanger

    Peter Hansen Guest

    Eric Belanger wrote:

    > Traceback (most recent call last):
    > File "./hnbupdate", line 42, in ?
    > localtousb()
    > File "./hnbupdate", line 12, in localtousb
    >
    > shutil.copyfile("/home/bilange/usb/.hnb","/home/bilange/.hnb-backup/"+str(int(time.time())))
    >
    > File "/usr/local/lib/python2.3/shutil.py", line 38, in copyfile
    > fdst = open(dst, 'wb')
    > IOError: [Errno 2] No such file or directory:
    > '/home/bilange/.hnb-backup/1087869914'


    There's the problem... you can't copy to a non-existent directory.
    I think in your first posting, you gave some misleading info, too.
    You said that using os.system('cp ' + file1 + ' ' + file2) showed
    a Python IOError, but I think it was this instead (with the full
    filename instead of ...):

    cp: cannot create regular file `/home/bilange/....': No such file or
    directory

    If that's true, the problem is the same there, though the error
    comes from the cp program and not from Python.

    What you probably want to do is use os.makedirs(). This will
    create the directory, even recursively, if needed. It normally
    throws an exception if the directory already exists though, so
    you probably want to put all this in a nice function:

    def mycopy(src, dst):
    '''copies one file to another, quietly creating the dest dir
    if required'''

    dstdir = os.path.split(dst)[0]
    try:
    os.makedirs(dstdir)
    except OSError:
    pass # dir exists, ignore error
    shutil.copyfile(src, dst)
    # optionally copy access/modification time as well
    shutil.copystat(src, dst)


    -Peter
    Peter Hansen, Jun 22, 2004
    #6
  7. Peter Hansen wrote:
    > There's the problem... you can't copy to a non-existent directory.


    I first doubted the problem was here, but I finally (after a few hours)
    noticed my directory (in the filesystem) had a typo in it (.hnb-bakcup).
    I never noticed it until this morning :S

    I guess I feel a little stupid, but hey, all that waste of time is worth
    a good laugh.

    Thank you :)
    Eric Belanger, Jun 22, 2004
    #7
  8. Eric Belanger

    Peter Hansen Guest

    Eric Belanger wrote:

    > I first doubted the problem was here, but I finally (after a few hours)
    > noticed my directory (in the filesystem) had a typo in it (.hnb-bakcup).
    > I never noticed it until this morning :S
    >
    > I guess I feel a little stupid, but hey, all that waste of time is worth
    > a good laugh.


    That _is_ funny. :)

    Use my idea about os.makedirs() and you won't have to make the folder
    manually any more...

    -Peter
    Peter Hansen, Jun 22, 2004
    #8
    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. Chad Crowder
    Replies:
    7
    Views:
    21,534
    hemant.yadav
    Oct 24, 2009
  2. ary
    Replies:
    0
    Views:
    664
  3. ary
    Replies:
    2
    Views:
    839
  4. Waldy
    Replies:
    4
    Views:
    494
    Waldy
    Aug 3, 2007
  5. Replies:
    2
    Views:
    451
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page