Python-禁止创建临时文件

问题描述 投票:0回答:1

我有一个不断运行的python进程,它会创建我不知道如何读取的临时文件。我已经尝试过使用带有'rb'的open和带有float和int的np.fromfile,但是结果没有意义。老实说,我不确定Python何时决定创建一个临时文件,但是无论如何,由于我无法解释数据,因此我可以一开始就阻止创建文件吗?他们填满硬盘驱动器,然后使进程崩溃。

**编辑:**时间是这样的,以至于我每次使用urllib.request从Internet下载文件时都会创建一个临时文件。video, _http = urllib.request.urlretrieve(video_path)

历史记录:我认为python正在创建临时文件,因为我运行了该实用程序(fnotifystat),该实用程序会告诉我/ tmp /目录中何时触摸文件。

Total   Open  Close   Read  Write   PID  Process         Pathname
152.0    2.0    1.0    6.0  143.0  18978 python          /tmp/tmpqsj_h99n
  4.0    1.0    1.0    0.0    2.0  18978 python          /tmp/x13i7gh5

Total   Open  Close   Read  Write   PID  Process         Pathname
 32.0    0.0    0.0   32.0    0.0  18978 python          /tmp/tmpqsj_h99n
python numpy temporary-files
1个回答
0
投票

urllib.request的源代码确实确实创建了delete参数设置为False的临时文件。

def urlretrieve(url, filename=None, reporthook=None, data=None):
    url_type, path = splittype(url)

    with contextlib.closing(urlopen(url, data)) as fp:
        headers = fp.info()

        # Just return the local path and the "headers" for file://
        # URLs. No sense in performing a copy unless requested.
        if url_type == "file" and not filename:
            return os.path.normpath(path), headers

        # Handle temporary file setup.
        if filename:
            tfp = open(filename, 'wb')
        else:
            tfp = tempfile.NamedTemporaryFile(delete=False)

对我来说,答案是一旦完成,就删除tmp文件。

© www.soinside.com 2019 - 2024. All rights reserved.