473,387 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

help with using temporary files

Hello

I'm sure its basic but I'm confused about the error I get with the
following code. Any help on basic tempfile usage?
ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on
Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.

from tempfile import NamedTemporaryFile

tmp = NamedTemporaryFile()
tmp.write("Hello")
tmp.close()

print tmp.name c:\docume~1\gerard\locals~1\temp\tmpxqn4yl
f = open(tmp.name)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory:
'c:\\docume~1\\gerard\\locals~1\\temp\\tmpxqn4yl'
Thanks

Gerard

Nov 22 '05 #1
1 3129
Gerard Flanagan wrote:
Hello

I'm sure its basic but I'm confused about the error I get with the
following code. Any help on basic tempfile usage?
ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on
Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.

from tempfile import NamedTemporaryFile

tmp = NamedTemporaryFile()
tmp.write("Hello")
tmp.close()

print tmp.name

c:\docume~1\gerard\locals~1\temp\tmpxqn4yl

f = open(tmp.name)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory:
'c:\\docume~1\\gerard\\locals~1\\temp\\tmpxqn4y l'
Thanks

Gerard


It gets created:

In [24]: import tempfile
In [25]: t = tempfile.NamedTemporaryFile()
In [26]: t.name
Out[26]: '/tmp/tmp9bmhap'
In [27]: ls -l /tmp/tmp*
-rw------- 1 jmjones jmjones 0 Nov 22 11:15 /tmp/tmp9bmhap

In [28]: t.write("123")

In [29]: t.flush()

In [30]: ls -l /tmp/tmp*
-rw------- 1 jmjones jmjones 3 Nov 22 11:15 /tmp/tmp9bmhap

In [31]: t.close()

In [32]: ls -l /tmp/tmp*
ls: /tmp/tmp*: No such file or directory

From the docstring, it gets automatically deleted on close:

def NamedTemporaryFile(mode='w+b', bufsize=-1, suffix="",
prefix=template, dir=None):
"""Create and return a temporary file.
Arguments:
'prefix', 'suffix', 'dir' -- as for mkstemp.
'mode' -- the mode argument to os.fdopen (default "w+b").
'bufsize' -- the buffer size argument to os.fdopen (default -1).
The file is created as mkstemp() would do it.

Returns an object with a file-like interface; the name of the file
is accessible as file.name. The file will be automatically deleted
when it is closed.
"""

HTH,

- jmj
Nov 22 '05 #2

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

Similar topics

8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
2
by: Mark | last post by:
I have one VS.NET 2003 solution that contains 3 projects. Two of the projects are ASP.NET projects each with their own .dll. The third project is a class library with its own .dll. We'll refer...
0
by: Blood... | last post by:
Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file...
1
by: Franko | last post by:
I get the following error. Please help c:\inetpub\wwwroot\WebApplication1\WebForm2.aspx(6,38): error CS1001: Identifier expected c:\inetpub\wwwroot\WebApplication1\WebForm2.aspx(6,52): error...
0
by: guy | last post by:
I have 2 problems with a VB.NET / ASP.NET app (2002 edition 1/Iget 'File or assembly name System.EnterpriseServices, or one of its dependencies, was not found. ' (this is required by a component) it...
0
by: Jie | last post by:
Does anyone know why I keep having the following error. I have to rebuild the solution every time I run it in design mode. thanks jie Server Error in '/ReapPortal' Application....
8
by: Chris | last post by:
Hi, All my websites and webservices are down. I get the following message Server Error in '/mywebservices' Application....
11
by: ulyses | last post by:
Let's assume I have following file: 2938929384902491233..... 923949919199191919112.... File contains INTs only. What is more they are huge. For example first row in file may contain integer...
1
by: keithb | last post by:
This application works without error in the development environment using the VS web server. When I publish the site to my IIS 6.0 server, I get this error at runtime when I open the Change...
7
by: rn5a | last post by:
This is the second time I am asking this question in this newsgroup since I haven't got a solution or response from anyone in my previous post & I need to resolve this issue desperately. Sorry for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.