473,659 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using NamedTemporaryD ir instead of multiple NamedTemporaryF iles

I am writing a library that creates temporary files and calls a series
of external programs to process these files. Sometimes these external
programs create files in the same directory as the input files, so to
make sure they are all deleted, one must create them in a temporary
directory, then delete it.

I've written a NamedTemporaryD ir class which is derived somewhat from
tempfile.NamedT emporaryFile in the standard library. Right now I am
using NamedTemporaryF ile to create individual files, but since I am
putting them in a directory that will be deleted anyway, I'm wondering
if I can simplify things (and not have to keep track of all fo the
NamedTemporaryF ile instances) by using tempfile.mkstem p() specifying my
temporary directory, and relying on the directory deletion when exiting
its with block.

Is there any reason I should keep track of each temporary files myself
instead of deleting the whole directory?

I am using Linux, but I would also be interested in cross-platform
considerations.

Also, the code is below. Is this worth submitting as a patch?

# NamedTemporaryF ile is based somewhat on Python 2.5.2
# tempfile._Tempo raryFileWrapper
#
# Original Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 Python
# Software Foundation; All Rights Reserved
#
# License at http://www.python.org/download/releases/2.5.2/license/

from tempfile import mkdtemp

class NamedTemporaryD ir(object):
def __init__(self, *args, **kwargs):
self.name = mkdtemp(*args, **kwargs)
self.close_call ed = False

def __enter__(self) :
return self

unlink = os.unlink

def close(self):
if not self.close_call ed:
self.close_call ed = True
self.unlink(sel f.name)

def __del__(self):
self.close()

def __exit__(self, exc, value, tb):
result = self.file.__exi t__(exc, value, tb)
self.close()
return result
Sep 9 '08 #1
3 1412
Please accept my apologies if this message was posted several times. My
newsreader claimed that a timeout error kept the message from being
posted, but I think it got through.
Sep 9 '08 #2
Michael Hoffman wrote:
unlink = os.unlink
Actually, I need to use shutil.rmtree instead, but you get the idea.
Sep 9 '08 #3
En Tue, 09 Sep 2008 15:49:32 -0300, Michael Hoffman
<4g*******@snea kemail.comescri bió:
I've written a NamedTemporaryD ir class which is derived somewhat from
tempfile.NamedT emporaryFile in the standard library. Right now I am
using NamedTemporaryF ile to create individual files, but since I am
putting them in a directory that will be deleted anyway, I'm wondering
if I can simplify things (and not have to keep track of all fo the
NamedTemporaryF ile instances) by using tempfile.mkstem p() specifying my
temporary directory, and relying on the directory deletion when exiting
its with block.
Looks fine...
def close(self):
if not self.close_call ed:
self.close_call ed = True
self.unlink(sel f.name)
Windows won't let you remove a non-empty directory.
def __exit__(self, exc, value, tb):
result = self.file.__exi t__(exc, value, tb)
self.close()
return result
self.file?

--
Gabriel Genellina

Sep 9 '08 #4

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

Similar topics

9
1974
by: Simon Harvey | last post by:
Hi all, In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the static method at the same time and interfere with one another. My question is, for each static method, do I need to lock access to only one call at a time? I've noticed that Microsofts Data Application block also uses static methods for its data access calls - for example execute...
4
12670
by: chris.dunigan | last post by:
I'm looking for an example of how to execute an existing DTS­ package from an ASP (VB)script and would appreciate any and all response. ­I don't even know if it's possible Thanks - Chuck Gatto Dan Guzman Apr 27 2000, 12:00 am show options
121
10025
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
19
676
by: Kamilche | last post by:
I have looked at many object-oriented programming frameworks out there for C. Though the ideas presented are intriguing, and I've used some of them in my own work, they all suffered some drawback or another. I have created a new method of doing OOP with C, something that fits my needs particularly well. I haven't encountered anything else like it, so I'm tossing it out to the Internet for comment. ...
1
1962
by: batista | last post by:
Hello all, I have a third praty grid control...named C1grid. Im using it in one of my apps.. Now, I have bind this grid to a custom dataset class named "DataViewEx". The code of the class is below... Now what happens is that im updating this dataviewex class from a separate thread.....
35
9335
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from 500000 to 3200000 of a file whose size is say 20MB... how do i request a download which starts directly at 500000th byte... thank u cheers
4
24337
by: Craggy | last post by:
I need to be able to add a record directly into a table using vba. The table is not the same table that is being used on my current form. Basically I want to have a question with a list box containing possible answers. Do a loop to capture these multiple responses and instead of storing them in a single field, separated by commas, store each selection as a different record in a table designed to handle responses. Each response will be...
68
4600
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a temporary but it was stated that it would not. This has come up in an irc channel but I can not find the original thread, nor can I get any code to work. Foo& Bar( int Val ) { return Foo( Val ); }
0
4051
by: David | last post by:
- Are there any peculiarities with using curs.executemany(...) vs. multiple How many times are you calling execute vs a single executemany? The python call overhead will add up for thousands of calls. The relevant source code is here if you're interested: http://svn.python.org/projects/python/trunk/Modules/_sqlite/cursor.c
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8339
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8535
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
8629
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
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
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
4176
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...
1
2757
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
1739
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.