temporary-files

How do I persist to disk a temporary file using Python?

折月煮酒 提交于 2019-12-03 06:44:31
问题 I am attempting to use the 'tempfile' module for manipulating and creating text files. Once the file is ready I want to save it to disk. I thought it would be as simple as using 'shutil.copy'. However, I get a 'permission denied' IOError: >>> import tempfile, shutil >>> f = tempfile.TemporaryFile(mode ='w+t') >>> f.write('foo') >>> shutil.copy(f.name, 'bar.txt') Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> shutil.copy(f.name, 'bar.txt') File "C:\Python25\lib

Rails - Creating temp files in a portable way

為{幸葍}努か 提交于 2019-12-03 06:30:06
问题 My rails application runs on a Ubuntu server machine. I need to create temporary files in order to "feed" them to a second, independent app (I'll be using rake tasks for this, in case this information is needed) My question is: what is the best way of creating temporary fields on a rails application? Since I'm in ubuntu, I could create them on /tmp/whatever , but what would work only in linux. I'd like my application to be as portable as possible - so it can be installed on Windows machines &

Python - How do I convert “an OS-level handle to an open file” to a file object?

风格不统一 提交于 2019-12-03 06:13:35
问题 tempfile.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. How do I convert that OS-level handle to a file object? The documentation for os.open() states: To wrap a file descriptor in a "file object", use fdopen(). So I tried: >>> import tempfile >>> tup = tempfile.mkstemp() >>> import os >>> f = os.fdopen(tup[0]) >>> f.write('foo\n') Traceback (most recent call last): File "

Do files created with Path.GetTempFileName get cleaned up automatically?

て烟熏妆下的殇ゞ 提交于 2019-12-03 05:38:04
I've always assumed the answer is yes, but now I'm trying to find the truth. When I create a temp file using Path.GetTempFileName() , will windows automatically clean that up later? What about if I create a directory under Path.GetTempPath() ? Will windows clean it up? Or is it the developer's responsibility to delete files created there? No they do not get deleted automatically. In order to create a file that will be deleted automatically when it is closed, pass FILE_FLAG_DELETE_ON_CLOSE to CreateFile . The file is to be deleted immediately after all of its handles are closed, which includes

How to create a temporary directory and get the path / file name in Python

久未见 提交于 2019-12-03 01:14:09
问题 how to create a temporary directory and get the path / file name in python 回答1: Use the mkdtemp() function from the tempfile module: import tempfile import shutil dirpath = tempfile.mkdtemp() # ... do stuff with dirpath shutil.rmtree(dirpath) 回答2: To expand on another answer, here is a fairly complete example which can cleanup the tmpdir even on exceptions: import contextlib import os import shutil import tempfile @contextlib.contextmanager def cd(newdir, cleanup=lambda: True): prevdir = os

Should Dispose() or Finalize() be used to delete temporary files?

人盡茶涼 提交于 2019-12-02 22:30:19
I have a class that makes use of temporary files ( Path.GetTempFileName() ) while it is active. I want to make sure these files do not remain on the user's hard drive taking up space after my program is closed. Right now my class has a Close() method which checks if any temporary files used by the class still exist and deletes them. Would it make more sense to put this code in the Dispose() or Finalize() methods instead? star Better yet would be to create the file with FileOptions.DeleteOnClose . This will ensure that the operating system forcibly deletes the file when your process exits (even

Deleting Temporary Files after usage

♀尐吖头ヾ 提交于 2019-12-02 20:55:35
I need to work with some temporary files in my Windows Forms .NET 3.5 application. Those files are opened in an external application that can of course be running for a longer time than my own program. Are there any best practices to make sure these temporary files are cleaned up at any time in order to avoid filling the user's hard disk with "junk" files which aren't needed anymore? Or does even Windows kind of handle this automatically? A nice example is any mail client: When you open an attachment in any application, it is usually written to a temporary file which is opened. Is there a way

Create temporary file and auto removed

*爱你&永不变心* 提交于 2019-12-02 20:53:23
I am writing a anti-leeching download script, and my plan is to create a temporary file, which is named by session ID, then after the session expires, the file will be automatically deleted. Is it possible ? And can you give me some tips how to do that in PHP ? Thanks so much for any reply So we have one or more files available for download. Creating a temporary file for each download requests is not a good idea. Creating a symlink() for each file instead is a much better idea. This will save loads of disk space and keep down the server load. Naming the symlink after the user's session is a

Rails - Creating temp files in a portable way

ⅰ亾dé卋堺 提交于 2019-12-02 20:18:35
My rails application runs on a Ubuntu server machine. I need to create temporary files in order to "feed" them to a second, independent app (I'll be using rake tasks for this, in case this information is needed) My question is: what is the best way of creating temporary fields on a rails application? Since I'm in ubuntu, I could create them on /tmp/whatever , but what would work only in linux. I'd like my application to be as portable as possible - so it can be installed on Windows machines & mac, if needed. Any ideas? Thanks a lot. tmp/ is definitively the right place to put the files. The

How to create a temporary directory and get the path / file name in Python

我怕爱的太早我们不能终老 提交于 2019-12-02 16:32:46
how to create a temporary directory and get the path / file name in python Philipp Use the mkdtemp() function from the tempfile module: import tempfile import shutil dirpath = tempfile.mkdtemp() # ... do stuff with dirpath shutil.rmtree(dirpath) To expand on another answer, here is a fairly complete example which can cleanup the tmpdir even on exceptions: import contextlib import os import shutil import tempfile @contextlib.contextmanager def cd(newdir, cleanup=lambda: True): prevdir = os.getcwd() os.chdir(os.path.expanduser(newdir)) try: yield finally: os.chdir(prevdir) cleanup() @contextlib