簡體   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