473,703 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

tempfile.NamedT emporaryFile wont work

On suse 9.3, tempfile.NamedT emporaryFile() doesnt work as expected.
(I found a permanent workaround, so I dont ask for help)
I expected to write to a file, and access it thru a shell command.
This code, in a loop:
tf = tempfile.NamedT emporaryFile()
tfName = tf.name
#tf.seek(0) # rewind the file
tf.write(chunk) ; tf.flush()
print >sys.stderr, '%s: %s' % (tfName, ['no',
'yes'][os.path.exists( tfName)])
subprocess.Pope n(['strings', tfName])

Symptom: the file does not always exist, after the call to
NamedTemporaryF ile(). Or at least its not seen by the strings command,
or by os.path.exists.

I guess the bug is pretty much os dependent, or even filesystem
dependent (Im on reiserfs). Maybe the os is buggy, maybe, somehow, the
python interface. Or did I miss something?
Shame, I didnt even try to check for a python bug tracker.
Nov 19 '06 #1
6 4685
Imbaud Pierre wrote:
tf = tempfile.NamedT emporaryFile()
tfName = tf.name
[...]
print >sys.stderr, '%s: %s' % (tfName, ['no',
'yes'][os.path.exists( tfName)])
subprocess.Pope n(['strings', tfName])
Just out of curiosity: Why did you assign tf.name to tfname?

Hypothetically, if tf.name changed, tfname wouldn't follow since
strings are immutable.

Regards,
Björn

--
BOFH excuse #149:

Dew on the telephone lines.

Nov 19 '06 #2
On Sun, 19 Nov 2006 13:11:13 +0100, Imbaud Pierre wrote:
On suse 9.3, tempfile.NamedT emporaryFile() doesnt work as expected.
[snip]
Symptom: the file does not always exist, after the call to
NamedTemporaryF ile(). Or at least its not seen by the strings command,
or by os.path.exists.

I guess the bug is pretty much os dependent, or even filesystem
dependent (Im on reiserfs). Maybe the os is buggy, maybe, somehow, the
python interface. Or did I miss something?
Shame, I didnt even try to check for a python bug tracker.
I can verify this problem occurs on Fedora Core 5 too:

import os
import sys
import tempfile
import subprocess
def test(n):
chunk = ': +++ abcd +++'
for i in xrange(n):
tf = tempfile.NamedT emporaryFile()
tfName = tf.name
tf.seek(0)
tf.write(str(i) + chunk)
tf.flush()
if not os.path.exists( tfName):
print 'pre-check: %s not there' % tfName
subprocess.Pope n(['strings', tfName])
if not os.path.exists( tfName):
print 'post-check: %s not there' % tfName
And here is a typical run, with the boring bits removed for ease of
reading:
>>test(30)
0: +++ abcd +++
1: +++ abcd +++
[ more of the same ]
14: +++ abcd +++
strings: '/tmp/tmpOALbx9': No such file
16: +++ abcd +++
17: +++ abcd +++
18: +++ abcd +++
[ more of the same ]
27: +++ abcd +++
strings: /tmp/tmpdc52Nz: No such file or directory
29: +++ abcd +++
Curiouser and curiouser... not only does os.path.exist always report the
temp file as existing (at least in my tests), even when strings can't find
it, but strings returns different error messages.

Is it possible this is a bug in strings?
--
Steven.

Nov 19 '06 #3
On Sun, 19 Nov 2006 13:18:39 +0100, Bjoern Schliessmann wrote:
Imbaud Pierre wrote:
> tf = tempfile.NamedT emporaryFile()
tfName = tf.name
[...]
print >sys.stderr, '%s: %s' % (tfName, ['no',
'yes'][os.path.exists( tfName)])
subprocess.Pope n(['strings', tfName])

Just out of curiosity: Why did you assign tf.name to tfname?

Hypothetically, if tf.name changed, tfname wouldn't follow since
strings are immutable.
Well, yes, but if tf.name changed, that won't change the file name on disk
either:
>>tf = tempfile.NamedT emporaryFile()
tf.name
'/tmp/tmpYVV1Ij'
>>os.path.exist s(tf.name)
True
>>oldname = tf.name
tf.name = "/tmp/something"
os.path.exist s(tf.name)
False
>>os.path.exist s(oldname)
True
I'm guessing that binding tf.name to tfName is a micro-optimization. In a
very tight loop, name lookups can take considerable time, and one
optimization can be to reduce the number of lookups:

method = something.metho d
while 1:
something.metho d # needs at least two lookups
method # needs a single lookup
--
Steve.

Nov 19 '06 #4
Steven D'Aprano wrote:
On Sun, 19 Nov 2006 13:11:13 +0100, Imbaud Pierre wrote:
>On suse 9.3, tempfile.NamedT emporaryFile() doesnt work as expected.
[snip]
>Symptom: the file does not always exist, after the call to
NamedTemporary File(). Or at least its not seen by the strings command,
or by os.path.exists.

I guess the bug is pretty much os dependent, or even filesystem
dependent (Im on reiserfs). Maybe the os is buggy, maybe, somehow, the
python interface. Or did I miss something?
Shame, I didnt even try to check for a python bug tracker.

I can verify this problem occurs on Fedora Core 5 too:

import os
import sys
import tempfile
import subprocess
def test(n):
chunk = ': +++ abcd +++'
for i in xrange(n):
tf = tempfile.NamedT emporaryFile()
tfName = tf.name
tf.seek(0)
tf.write(str(i) + chunk)
tf.flush()
if not os.path.exists( tfName):
print 'pre-check: %s not there' % tfName
subprocess.Pope n(['strings', tfName])
if not os.path.exists( tfName):
print 'post-check: %s not there' % tfName
And here is a typical run, with the boring bits removed for ease of
reading:
>>>test(30)
0: +++ abcd +++
1: +++ abcd +++
[ more of the same ]
14: +++ abcd +++
strings: '/tmp/tmpOALbx9': No such file
16: +++ abcd +++
17: +++ abcd +++
18: +++ abcd +++
[ more of the same ]
27: +++ abcd +++
strings: /tmp/tmpdc52Nz: No such file or directory
29: +++ abcd +++
Curiouser and curiouser... not only does os.path.exist always report the
temp file as existing (at least in my tests), even when strings can't find
it, but strings returns different error messages.

Is it possible this is a bug in strings?
What /you/ are seeing is not a bug, I think. Popen() is asynchronous,
therefore you may enter the second iteration -- which implicitly closes the
temporary file -- before strings actually tries to access it. Use call()
and everything should be fine.

Peter

Nov 19 '06 #5
Steven D'Aprano a écrit :
On Sun, 19 Nov 2006 13:18:39 +0100, Bjoern Schliessmann wrote:

>>Imbaud Pierre wrote:

>> tf = tempfile.NamedT emporaryFile()
tfName = tf.name
[...]
print >sys.stderr, '%s: %s' % (tfName, ['no',
'yes'][os.path.exists( tfName)])
subprocess.Pope n(['strings', tfName])

Just out of curiosity: Why did you assign tf.name to tfname?

Hypotheticall y, if tf.name changed, tfname wouldn't follow since
strings are immutable.

Well, yes, but if tf.name changed, that won't change the file name on disk
either:

>>>>tf = tempfile.NamedT emporaryFile()
tf.name
'/tmp/tmpYVV1Ij'
>>>>os.path.exi sts(tf.name)
True
>>>>oldname = tf.name
tf.name = "/tmp/something"
os.path.exi sts(tf.name)
False
>>>>os.path.exi sts(oldname)
True
I'm guessing that binding tf.name to tfName is a micro-optimization.
indeed. And I dont see why tf.name would change.
In a
very tight loop, name lookups can take considerable time, and one
optimization can be to reduce the number of lookups:

method = something.metho d
while 1:
something.metho d # needs at least two lookups
method # needs a single lookup
Nov 19 '06 #6
Peter Otten a écrit :
Steven D'Aprano wrote:

>>On Sun, 19 Nov 2006 13:11:13 +0100, Imbaud Pierre wrote:

>>>On suse 9.3, tempfile.NamedT emporaryFile() doesnt work as expected.

[snip]

>>>Symptom: the file does not always exist, after the call to
NamedTempora ryFile(). Or at least its not seen by the strings command,
or by os.path.exists.

I guess the bug is pretty much os dependent, or even filesystem
dependent (Im on reiserfs). Maybe the os is buggy, maybe, somehow, the
python interface. Or did I miss something?
Shame, I didnt even try to check for a python bug tracker.

I can verify this problem occurs on Fedora Core 5 too:

import os
import sys
import tempfile
import subprocess
def test(n):
chunk = ': +++ abcd +++'
for i in xrange(n):
tf = tempfile.NamedT emporaryFile()
tfName = tf.name
tf.seek(0)
tf.write(str( i) + chunk)
tf.flush()
if not os.path.exists( tfName):
print 'pre-check: %s not there' % tfName
subprocess.Po pen(['strings', tfName])
if not os.path.exists( tfName):
print 'post-check: %s not there' % tfName
And here is a typical run, with the boring bits removed for ease of
reading:

>>>>>test(30)

0: +++ abcd +++
1: +++ abcd +++
[ more of the same ]
14: +++ abcd +++
strings: '/tmp/tmpOALbx9': No such file
16: +++ abcd +++
17: +++ abcd +++
18: +++ abcd +++
[ more of the same ]
27: +++ abcd +++
strings: /tmp/tmpdc52Nz: No such file or directory
29: +++ abcd +++
Curiouser and curiouser... not only does os.path.exist always report the
temp file as existing (at least in my tests), even when strings can't find
it, but strings returns different error messages.

Is it possible this is a bug in strings?

What /you/ are seeing is not a bug, I think. Popen() is asynchronous,
therefore you may enter the second iteration -- which implicitly closesthe
temporary file -- before strings actually tries to access it. Use call()
and everything should be fine.
Thanks A LOT, works fine, I feel kind of silly; your diagnostic is
pretty obvious, afterward... I felt uneasy not closing the Popen, but
it worked, so why bother? Its so easy to make ugly code!

Nov 19 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3822
by: Pierre Rouleau | last post by:
Hi all! I am using Python 2.3.1 on Win32 (NT, 2000). Whenever a file imports the standard tempfile module, Python 2.3.1 issues the following warning: C:\Python23\lib\fcntl.py:7: DeprecationWarning: the FCNTL module is Deprecated; please use fcntl DeprecationWarning).
2
1968
by: cherico | last post by:
from tempfile import * f = NamedTemporaryFile () f = write ( 'test' ) print f.read () #here print nothing why is it nothing to read()? I suppose f is of mode 'w+'
2
4088
by: marco | last post by:
Hello, I having a problem creating directories with Python 2.3.4 (compiled with gcc 3.2.2, under Linux 2.4.20-31.9). I'm writing a plugin which works in the following way: GUI -- talks to --> Perl backend Perl backend -- talks to --> Python plugin Python plugin -- talks to --> Python script
5
5430
by: Lee Harr | last post by:
Is there any other reason to use a named tempfile other than to be able to open it again? I am trying to understand this section of the documentation regarding NamedTemporaryFile: """ Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later) """
5
1706
by: Gregory Piñero | last post by:
Hey group, I have a command line tool that I want to be able to call from a Python script. The problem is that this tool only writes to a file. So my solution is to give the tool a temporary file to write to and then have Python read that file. I figure that's the safest way to deal with this sort of thing. (But I'm open to better methods). Here's my code so far, could anyone tell me the proper way to use
6
1390
by: James T. Dennis | last post by:
Tonight I discovered something odd in the __doc__ for tempfile as shipped with Python 2.4.4 and 2.5: it says: This module also provides some data items to the user: TMP_MAX - maximum number of names that will be tried before giving up. template - the default prefix for all temporary names. You may change this to control the default prefix.
9
3750
by: billiejoex | last post by:
Hi there. I'm trying to generate a brand new file with a unique name by using tempfile.mkstemp(). In conjunction I used os.fdopen() to get a wrapper around file properties (write & read methods, and so on...) but 'name' attribute does not contain the correct file name. Why? <fdopen> Moreover, I'd like to know if I'm doing fine. Does this approach avoid
4
1550
by: samir.vds | last post by:
Hello everyone, I'm trying to test the tempfile module with the following script, which basically creates a temporary file, fills the file with some test data and prints it. import tempfile t = tempfile.TemporaryFile() t.write("lalalala")
7
578
by: byte8bits | last post by:
Wondering if someone would help me to better understand tempfile. I attempt to create a tempfile, write to it, read it, but it is not behaving as I expect. Any tips? <open file '<fdopen>', mode 'w+b' at 0xab364968> 0 0 0
0
9266
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9020
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8972
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5926
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3128
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2467
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2073
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.