繁体   English   中英

Python> 3中的tempfile.TemporaryFile和tempfile.NamedTemporaryFile是否相同

[英]Are tempfile.TemporaryFile and tempfile.NamedTemporaryFile same in Python > 3

考虑一下

import tempfile

print(help(tempfile.TemporaryFile))
print(help(tempfile.NamedTemporaryFile))

上面的2条语句给出与输出相同的东西,它们是否都属于同一类NamedTemporaryFile

tp = tempfile.TemporaryFile(suffix="xyz.txt", prefix="abc",
                            dir="C:/bin/", mode='w',
                            buffering=-1, delete=False)
print(tp)
tp.close()

tp = tempfile.NamedTemporaryFile(suffix="pqr.txt", prefix="ghi",
                                 dir="C:/bin/", mode='w',
                                 buffering=-1, delete=False)
print(tp)
tp.close()

同样在上面的代码中,这两个类都期望相同的命名/关键字参数在Python> 3中都相同?

这取决于您的平台。 当我阅读https://github.com/python/cpython/blob/eb126eddbd7542ac9d7cd2736116aee2e0bd03dd/Lib/tempfile.py#L560中的代码时

if _os.name != 'posix' or _os.sys.platform == 'cygwin':
    # On non-POSIX and Cygwin systems, assume that we cannot unlink a file
    # while it is open.
    TemporaryFile = NamedTemporaryFile

对于非posix(Linux等)环境或cygwin,这些方法相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM