Re: [Tutor] tempfile and webbrowser

2006-09-10 Thread yves
Kent Johnson a écrit :

Hello,

 Try it like this, using os.fdopen() to convert the low-level file handle 
 from mkstemp() to a Python file object:
 
 In [21]: fd, fname = tempfile.mkstemp()
 
 In [22]: f = os.fdopen(fd, 'w')
 
 In [23]: f.write('foo')
 
 In [24]: f.close()
 
 In [25]: os.unlink(fname)
 
 Seems to work...

Yes, indeed, it works.
Not so easy for me to understand, though. I think I get it, more or
less, with the help of the Python tempfile module documentation and the 
help of the Wikipedia article on file descriptors:
http://en.wikipedia.org/wiki/File_descriptor

Thank you.
-- 
Yves Egrix

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tempfile and webbrowser

2006-09-10 Thread Kent Johnson
yves wrote:
 Kent Johnson a écrit :
 
 Hello,
 
 Try it like this, using os.fdopen() to convert the low-level file handle 
 from mkstemp() to a Python file object:

 In [21]: fd, fname = tempfile.mkstemp()

 In [22]: f = os.fdopen(fd, 'w')

 In [23]: f.write('foo')

 In [24]: f.close()

 In [25]: os.unlink(fname)

 Seems to work...
 
 Yes, indeed, it works.
 Not so easy for me to understand, though. I think I get it, more or
 less, with the help of the Python tempfile module documentation and the 
 help of the Wikipedia article on file descriptors:
 http://en.wikipedia.org/wiki/File_descriptor

OK...the problem was that mkstemp() was opening the file and returning a 
low-level object that references the open file. You were opening the 
file a second time, so it would have to be closed twice before it could 
be deleted.

The object returned by mkstemp is actually just an integer called a file 
handle. This is the way C refers to open files. The call to os.fdopen() 
wraps the low-level file handle with a Python file object which you can 
then use just as if you opened it yourself.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tempfile and webbrowser

2006-09-08 Thread Alan Gauld
 The file deletion (os.unlink(f.name)) does not work on Windows (it 
 works on Ubuntu with Python 2.4, though).

 So, is there a way to get this os.unlink(f.name) to work on Windows?

Use os.remove() instead.

Alan G. 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tempfile and webbrowser

2006-09-08 Thread Kent Johnson
Alan Gauld wrote:
 The file deletion (os.unlink(f.name)) does not work on Windows (it 
 works on Ubuntu with Python 2.4, though).

 So, is there a way to get this os.unlink(f.name) to work on Windows?
 
 Use os.remove() instead.

os.remove() and os.unlink() are identical according to the docs; if you 
look at posix_module.c you can see this is true - they both map to 
posix_unlink().

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tempfile and webbrowser

2006-09-08 Thread Alan Gauld
 Use os.remove() instead.

 os.remove() and os.unlink() are identical according to the docs; if 
 you look at posix_module.c you can see this is true - they both map 
 to posix_unlink().

 Kent

Maybe so, but os.remove() works on my XP box... :-)

I didn't try unlink since the OP said it didn't work. And maybe 
significantly
MS Visual C doesn't (or didn't up to v6) include the unlink system 
call.
So I figured maybe remove was implemented to be system specific.

But it was not a scientific test.

Alan G. 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tempfile and webbrowser

2006-09-08 Thread Kent Johnson
Alan Gauld wrote:
 Use os.remove() instead.
 os.remove() and os.unlink() are identical according to the docs; if 
 you look at posix_module.c you can see this is true - they both map 
 to posix_unlink().

 Kent
 
 Maybe so, but os.remove() works on my XP box... :-)
 
 I didn't try unlink since the OP said it didn't work. 

Did you try the OP's code with os.remove()? I doubt it works for you, 
here is what I got:

In [9]: import os, tempfile

In [10]: a = tempfile.mkstemp()

In [11]: f= open(a[1],'w')

In [12]: f.write(foo)

In [13]: f.close()

In [14]: print f.name
c:\docume~1\ktjohn~1\locals~1\temp\tmpy_3mmh

In [15]: # here some code to do some things with f

In [16]: os.unlink(f.name)
---
exceptions.OSError
Traceback (most recent call last)

D:\Projects\e3po\ipython console

OSError: [Errno 13] Permission denied: 
'c:\\docume~1\\ktjohn~1\\locals~1\\temp\\tmpy_3mmh'


os.remove() doesn't work either:

In [17]: os.remove(f.name)
---
exceptions.OSError
Traceback (most recent call last)

D:\Projects\e3po\ipython console

OSError: [Errno 13] Permission denied: 
'c:\\docume~1\\ktjohn~1\\locals~1\\temp\\tmpy_3mmh'


Maybe time for another trip to the docs:
mkstemp() returns a tuple containing an OS-level handle to an open file 
(as would be returned by os.open()) and the absolute pathname of that 
file, in that order.

Aha, mkstemp() is already opening the file, so the explicit open() 
creates a *second* handle to the open file; when it is closed, the 
original handle is still open.

Try it like this, using os.fdopen() to convert the low-level file handle 
from mkstemp() to a Python file object:

In [21]: fd, fname = tempfile.mkstemp()

In [22]: f = os.fdopen(fd, 'w')

In [23]: f.write('foo')

In [24]: f.close()

In [25]: os.unlink(fname)

Seems to work...

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tempfile and webbrowser

2006-09-07 Thread yves
Hello,

Here is another problem:
Considering this programm:

import os, tempfile

a = tempfile.mkstemp()
f= open(a[1],'w')
f.write(foo)
f.close()
print f.name
# here some code to do some things with f
os.unlink(f.name)

The output is:
Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on
win32
Type help, copyright, credits or license for more information.

  ## working on region in file

c:/DOCUME~1/Yves/LOCALS~1/Temp/python-1408y8u...
c:\docume~1\yves\locals~1\temp\tmp8fr4-9
Traceback (most recent call last):
   File stdin, line 1, in ?
   File c:/DOCUME~1/Yves/LOCALS~1/Temp/python-1408y8u, line 8, in ?
 os.unlink(f.name)
OSError: [Errno 13] Permission denied:
'c:\\docume~1\\yves\\locals~1\\temp\\tmp8fr4-9'

 

The file deletion (os.unlink(f.name)) does not work on Windows (it works 
  on Ubuntu with Python 2.4, though).

So, is there a way to get this os.unlink(f.name) to work on Windows?

-- 
Yves Egrix

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tempfile and webbrowser

2006-09-03 Thread Kent Johnson
yves wrote:
 Hello tutors,

 This programm works:
 **
 import webbrowser
 a = open('test.htm','wb')
 a.write(htmlTest/html)
 webbrowser.open(a.name)
 a.close()
 ***
 but I would like to avoid the risk of overwriting an already existing
 test.htm file, so I try to use the module tempfile:
 ***
 import tempfile
 import webbrowser
 a = tempfile.NamedTemporaryFile('w+b',-1,'.html')
 a.write(htmlTest/html)
 webbrowser.open(a.name)
 #a.close()
 **
 This does not work (no traceback error though): the browser displays a
 blank page.

The problem is that the file is never actually written because you omit 
the close. But when you do close the temp file, it is deleted. Try using 
a.flush() instead of a.close(), that will force the file to be written. 
Alternately use tempfile.mkstemp() which lets you close the file and 
delete it when you are done with it.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tempfile and webbrowser

2006-09-03 Thread yves
Kent Johnson a écrit :

 The problem is that the file is never actually written because you omit 
 the close. But when you do close the temp file, it is deleted. Try using 
 a.flush() instead of a.close(), that will force the file to be written. 
 Alternately use tempfile.mkstemp() which lets you close the file and 
 delete it when you are done with it.

Thank you,
this works:

import tempfile
import webbrowser

a = tempfile.mkstemp('.html')
f= open(a[1],'w')
f.write(htmlTest/html)
f.close()
webbrowser.open(f.name)
*
-- 
Yves Egrix
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tempfile and webbrowser

2006-09-03 Thread Alan Gauld
 This program works:
 **
 import webbrowser
 a = open('test.htm','wb')

Any particular feason to open the file in binary mode? 
That can sometimes cause odd things to happen.

 a.write(htmlTest/html)
 webbrowser.open(a.name)
 a.close()

The close should come before the browser reads 
the file, otherwise you are trying to read a file thats 
still open in write mode and the behaviouir there is 
undefined on most operating systems.

 import tempfile
 import webbrowser
 a = tempfile.NamedTemporaryFile('w+b',-1,'.html')

Now you are making it even more complex by using 
a read/write mode binary temporary file!

 a.write(htmlTest/html)
 webbrowser.open(a.name)
 #a.close()
 **

 Have you got some suggestions to tackle this problem?

Simplify the file handling to use text files and close the file 
as soon as possible. These are good guidelines for any 
file handling you do.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor