temporary-files

Generate temporary file names without creating actual file in Python

心已入冬 提交于 2020-08-21 04:25:44
问题 The question, number 10501247, in stackoverflow gives answer how to create temporary file in Python. I only need to have temporary file name in my case. Calling tempfile.NamedTemporaryFile() returns file handle after actual file creation. Is there way to get file name only? # Trying to get temp file path tf = tempfile.NamedTemporaryFile() temp_file_name = tf.name tf.close() # Here is my real purpose to get the temp_file_name f = gzip.open(temp_file_name ,'wb') ... 回答1: If you want a temp file

tempfile.TemporaryFile() does not work with sqlite3 after being assigned to a variable

穿精又带淫゛_ 提交于 2020-08-06 01:39:11
问题 I am using the tempfile and sqlite3 modules in Python. The following code works: import sqlite3, tempfile conn1 = sqlite3.connect(tempfile.TemporaryFile().name) Thus, I would expect the following code to work as well, but it doesn't: import sqlite3, tempfile database_file = tempfile.TemporaryFile() conn2 = sqlite3.connect(database_file.name) sqlite3.OperationalError: unable to open database file I was able to use this answer to extract the file path used by conn1. Feeding that into sqlite

PyTest: Auto delete temporary directory created with tmpdir_factory

不想你离开。 提交于 2020-08-05 05:35:19
问题 I'm trying to create a temporary directory with a specific name (eg, "data") for all tests within a module using PyTest's tmpdir_factory similar to the tutorial: @pytest.fixture(scope='module') def project_file(self, tmpdir_factory): return tmpdir_factory.mktemp("data") I'm using the temporary directory in some tests within the module successfully. However, the directory still exists after running the tests and when I run them again, I get a failure because I cannot create a new temporary

Python Subprocess: how/when do they close file?

独自空忆成欢 提交于 2020-04-18 04:01:42
问题 I wonder why subprocesses keep so many files open. I have an example in which some files seem to remain open forever (after the subprocess finishes and even after the program crashes). Consider the following code: import aiofiles import tempfile async def main(): return [await fds_test(i) for i in range(2000)] async def fds_test(index): print(f"Writing {index}") handle, temp_filename = tempfile.mkstemp(suffix='.dat', text=True) async with aiofiles.open(temp_filename, mode='w') as fp: await fp

Is it possible to get the path of a tempfile in Python 3

狂风中的少年 提交于 2020-04-12 16:47:00
问题 I was wondering if it was possible to get the file path of a temporary file made using the tempfile library. Basically, I'm trying to make a function that intakes some data, and generates a temporary csv file based off of said data. I was wondering if there was a way to get the path of this temporary file? 回答1: Use tempfile.NamedTemporaryFile to create a temporary file with a name, and then use the .name attribute of the object. Note that there are platform-specific limitations on how this

Azure Web App Temp file cleaning responsibility

烈酒焚心 提交于 2020-04-10 07:37:11
问题 In one of my Azure Web App Web API application, I am creating temp files using this code in a Get method string path = Path.GetTempFileName(); // do some writing on this file. then read var fileStream = File.OpenRead(path); // then returning this stream as a HttpResponseMessage response My question is, in a managed environment like this (not in VM), do I need to clear those temporary files by myself? Shouldn't Azure itself supposed to clear those temp files? 回答1: Those files only get cleaned