tempfile.NamedTemporaryFile创建临时文件在windows没有权限打开

记录下来是因为当时谷歌这个问题时发现,网上也有很多人遇到这个问题,我也因为这个问题导致了一个bug,所以告诫自己以后使用API多仔细看看文档。

python的tempfile模块用于创建系统临时文件,是一个很有用的模块。通过tempfile.NamedTemporaryFile,可以轻易的创建临时文件,并返回一个文件对象,文件名可以通过对象的name属性获取,且创建的临时文件会在关闭后自动删除。下面这段python代码创建一个临时文件,并再次打开该临时文件,写入数据,然后再次打开,读取文件,并按行打印文件内容。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tempfile


# create tmp file and write it
tmp_file = tempfile.NamedTemporaryFile()
print 'tmp file is {self.name}'.format(self=tmp_file)

with open(tmp_file.name, 'w') as f:
    f.write("line 1\nline 2\nline 3\n")


# user tmp file
with open(tmp_file.name) as f:
    for line in f.readlines():
        print line

在linux上运行上面的python代码,会创建一个临时文件,且程序退出后该临时文件会自动删除,输出如下:

root@master:demo$ python tmp_file.py
tmp file is /tmp/tmpb3EYGV
line 1

line 2

line 3

但是在windows上运行时,提示没有权限,不能打开创建的临时文件,是不是感觉很奇怪。

E:\share\git\python_practice\demo>tmp_file.py
tmp file is c:\users\leo\appdata\local\temp\tmphn2kqj
Traceback (most recent call last):
  File "E:\share\git\python_practice\demo\tmp_file.py", line 11, in <module>
    with open(tmp_file.name, 'w') as f:
IOError: [Errno 13] Permission denied: 'c:\\users\\leo\\appdata\\local\\temp\\tm
phn2kqj'

查看官方文档,该API解释如下:

tempfile.NamedTemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None[, delete=True]]]]]])
This function operates exactly as TemporaryFile() does, except that the file is guaranteed to have a visible name in the file system (on Unix, the directory entry is not unlinked). That name can be retrieved from the name attribute of the returned file-like object. Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later). If delete is true (the default), the file is deleted as soon as it is closed.
The returned object is always a file-like object whose file attribute is the underlying true file object. This file-like object can be used in a with statement, just like a normal file.
New in version 2.3.
New in version 2.6: The delete parameter.

注意其中的一句话:

Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).

大概意思是,当这个临时文件处于打开状态,在unix平台,该名字可以用于再次打开临时文件,但是在windows不能。所以,如果要在windows打开该临时文件,需要将文件关闭,然后再打开,操作完文件后,再调用os.remove删除临时文件。

参考:https://docs.python.org/2/library/tempfile.html
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,997评论 19 139
  • Python 面向对象Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对...
    顺毛阅读 4,250评论 4 16
  • 一、温故而知新 1. 内存不够怎么办 内存简单分配策略的问题地址空间不隔离内存使用效率低程序运行的地址不确定 关于...
    SeanCST阅读 7,884评论 0 27
  • 一直来想写点什么,想想最近的一次认真的写东西应该是帮一个人写东西到凌晨三四点。 或许我该说点什么。解释?辩解?总结...
    lielove阅读 205评论 0 0
  • 一本书,一杯茶,一份小吃,足以让你沉浸其中,那些高楼大厦,那些车流人海,都仿佛随着时光悄然远去…… 2...
    陌上花开一水间阅读 847评论 14 18