Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Cannot create a new file

Reply
Thread Tools

Cannot create a new file

 
 
Eric Belanger
Guest
Posts: n/a
 
      06-22-2004
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.
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      06-22-2004
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
 
Reply With Quote
 
 
 
 
John
Guest
Posts: n/a
 
      06-22-2004
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.
 
Reply With Quote
 
Eric Belanger
Guest
Posts: n/a
 
      06-22-2004
> 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)
 
Reply With Quote
 
Bryan
Guest
Posts: n/a
 
      06-22-2004
> #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
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      06-22-2004
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
 
Reply With Quote
 
Eric Belanger
Guest
Posts: n/a
 
      06-22-2004
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
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      06-22-2004
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
 
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
System.IO.IOException: Cannot create a file when that file already exists. Chad Crowder ASP .Net 7 10-24-2009 06:39 AM
Cannot debug web application "Cannot create/shadow copy ..." Waldy ASP .Net 4 08-03-2007 01:36 PM
Cannot delete a file the process cannot access the file because it is being used by another process ASp.net 2 ary ASP .Net 0 11-28-2006 09:13 AM
Cannot Delete File: Cannot read from source file or disk dwcn Computer Support 7 09-14-2005 10:51 PM
The file 'bin\WebProj.dll' cannot be copied to the run directory. The requested operation cannot be performed on a file with a user-mapped section open. J Gao ASP .Net 0 07-28-2003 04:18 PM



Advertisments