没有写权限时,Python tempfile.TemporaryFile 在 Windows 上挂起 [英] Python tempfile.TemporaryFile hangs on Windows when no write privilege

查看:19
本文介绍了没有写权限时,Python tempfile.TemporaryFile 在 Windows 上挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的环境是 Python 3.7.2,在 Windows 10 上运行.我正在研究目录选择小部件,我正在寻找最干净+最可靠的方法来测试所选目录路径是否允许写入权限.

以前我一直通过通常的 open() 方法打开一个命名文件,向它写入几个字节,然后删除它——把整个东西放在一个 try-除了块.这没关系,但它冒着留下不需要的文件的风险.最近我看到了 tempfile.TemporaryFile() 的文档,这似乎是获得相同结果的更简洁的方法,而且没有在系统上留下垃圾文件的风险.

问题是,tempfile.TemporaryFile() 挂在我的系统上,当它被赋予一个只读文件夹的 dir 参数时.我在谷歌上搜索并发现了

有谁知道可能是什么问题?特别是这是一个 Python 错误,还是我对 TemporaryFile 的使用有问题?

如果有帮助,以下是 Windows 为每个文件夹显示的特定权限:

解决方案

比我最初做的更深入,找到了答案.这确实是 一个 Python 错误,前段时间报告过,但仍有待解决.

来自 eryksun 的评论描述了细节——这也是促使我仔细研究 Python 错误数据库的原因——所以最终这就是功劳所在.我只是在这里填写它来回答问题并结束.

该错误仅影响 Windows 环境,但不幸的是,对于此常见用例,它导致 tempfile.TemporaryFile 在 Windows 上无法使用.

My environment is Python 3.7.2, running on Windows 10. I'm working on a directory-selection widget, and I'm looking for the cleanest+surest method to test whether the selected directory path allows write privilege.

Previously I'd been opening a named file by the usual open() method, writing a few bytes to it, then deleting it -- putting the whole thing in a try-except block. This was OK but it ran the risk of leaving behind an unwanted file. Recently I came across the documentation for tempfile.TemporaryFile(), and this seemed cleaner way to get the same result, with no risk of leaving junk files on the system.

The problem is, tempfile.TemporaryFile() hangs on my system when it's given a dir parameter that's a read-only folder. I've googled around and found this very old bug, but it was written against Python 2.4 and was fixed long ago.

Here's a test script I put together to illustrate the issue. (Note that I've omitted the file-delete that my actual app performs, as it's not relevant for the illustration.)

import os, tempfile

def normOpen(checkPath):
    try:
        with open(os.path.join(checkPath,'x.txt'),'wb') as tf:
            tf.write(b'ABC')
    except Exception as e:
        print('Write disabled for '+checkPath)
        print(str(e))
    else:
        print('Write enabled  for '+checkPath)

def tfOpen(checkPath):
    try:
        with tempfile.TemporaryFile(dir=checkPath) as tf:
            tf.write(b'ABC')
    except Exception as e:
        print('Write disabled for '+checkPath)
        print(str(e))
    else:
        print('Write enabled  for '+checkPath)

tryPath1 = 'C:\\JDM\\Dev_Python\\TMPV\\canwrite'  #Full control path
tryPath2 = 'C:\\JDM\\Dev_Python\\TMPV\\nowrite'   #Read-only path

print('First method - normal file-open')
normOpen(tryPath1)
normOpen(tryPath2)

print('Second method - TemporaryFile')
tfOpen(tryPath1)
tfOpen(tryPath2)

When I run this script, it hangs on the last line and just sits there (Task Manager shows Python consuming about 10-15% CPU).

Does anyone know what the problem might be? Particularly is this a Python bug, or is there something wrong with my usage of TemporaryFile?

In case it helps, below is the specific privileges Windows shows for each of these folders:

解决方案

A deeper dive than I'd initially done, turned up the answer. This is indeed a Python bug, reported some time ago but which remains to be addressed.

The comments from eryksun describe the details -- and it's what prompted me to take a closer look at the Python bug database -- so ultimately that's where credit is due. I'm just filling it in here to get the question answered and closed out.

The bug affects only Windows environments, but unfortunately it has the result of rendering tempfile.TemporaryFile unusable on Windows for this common use case.

这篇关于没有写权限时,Python tempfile.TemporaryFile 在 Windows 上挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆