470,636 Members | 1,601 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

Cannot create a new file

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. :)
Jul 18 '05 #1
7 6373
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
Jul 18 '05 #2
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.
Jul 18 '05 #3
> 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)
Jul 18 '05 #4
> #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
Jul 18 '05 #5
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
Jul 18 '05 #6
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 :)
Jul 18 '05 #7
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
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Christopher Brandsdal | last post: by
2 posts views Thread by Jay Bienvenu | last post: by
reply views Thread by Patrick F | last post: by
1 post views Thread by Patrick F | last post: by
reply views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post: by
2 posts views Thread by karinmorena | last post: by
1 post views Thread by Korara | last post: by
???
reply views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.