简体   繁体   中英

Are tempfile.TemporaryFile and tempfile.NamedTemporaryFile same in Python > 3

Consider this

import tempfile

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

The above 2 statements give same thing as the output, does both of them belong to same class 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()

Also in the above code both these classes expect the same named/keyword arguments are both of them same in Python > 3???

That depends on your platform. As I read the code in 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

For non posix (linux, etc) environments or cygwin these methods are the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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