Python文件对象是一个真实的文件吗? [英] Is a Python File object a real file?

查看:121
本文介绍了Python文件对象是一个真实的文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,文件对象可能指向一个真实的文件,标准输入,标准输出,标准错误,甚至是其他的东西。因此,它可以是类文件真实文件,如 file.flush



如何知道文件是否是真实文件?我发现了一些猜测方法,但没有一个看起来真的可靠:


  1. 文件不在[sys.stdin,sys。 stdout,sys.stderr] - 看起来最安全,但只适用于那些标准类型,不是非常通用的解决方案。
  2. os.path。 isfile(file.name) - 看起来相当安全,但是如果在某些创建模式下打开新文件,可能无法正常工作。 > file.fileno()== 0 - 这个假设没有实现方法,默认的实现返回0,正常的实现永远不会返回0.

  3. file.name.startswith('<') - 这真的假设文件系统不允许< 在文件名中。

为什么我需要知道这个,是因为我想尽早关闭它并使用 file with f:可能会关闭标准输入/标准输出,这听起来像是一个糟糕的主意。

一般来说,你应该使用和...作为f: only s也就是说,使用文件句柄打开,或者使用语句获取





在UNIX上,您可以检查 .fileno() - 0代表standard in,1标准出2,标准误;一般你不应该关闭这些流,因为没有办法重新打开它们了。




file.name 并不是真正的100%证明方法,因为许多流可以有一个名称,即使它们不是真实的文件,而且这个特殊的名字可以与磁盘上的现有文件冲突:

 >>> import os.path 
>>> os.path.exists(sys.stdin.name)
True
>>> sys.stdin.name $ b $< stdin>'

我也做了 touch'< stdin>'

另外,在UNIX中习惯性地将临时文件打开它们之后,所以它们不再以这个名字存在。另一方面,甚至一个现有的文件在被打开之后可能会被重命名为,因此不存在原来的名字。






如果你想知道标准流是否被重定向到一个文件,你可以这样做:

 >>> import os 
>>> import stat
>>> stat.S_ISREG(os.fstat(sys.stdout.fileno())。st_mode)
False



如果模式描述的是一个普通的文件(这里输出到终端,所以它是假的) S_ISREG
返回 );同样你也可以通过 isatty 找出是否有文件被重定向到终端

 >>> os.isatty(sys.stdout.fileno())
True


In Python a file object may point to a real file, stdin, stdout, stderr, or even something else. So it can be file-like, or a real file as stated on file.flush.

How to know if a file is a real file? I found some ways to guess but none seems really reliable:

  1. file not in [sys.stdin, sys.stdout, sys.stderr] -- Seems safest but only for those standard types, not very generic solution.
  2. os.path.isfile(file.name) -- Seems pretty safe but might not work if new file is opened in some creation modes I guess.
  3. file.fileno() == 0 -- This supposes the method isn't implemented, default implementation returns 0, and normal implementations never return 0.
  4. file.name.startswith('<') -- This really supposes the file system doesn't allow < in the file name.

Why I need to know this, is because I'd like to close it early and using with file as f: may close stdin/stdout which sounds like a bad idea.

解决方案

Generally you should use the with ... as f: only symmetrically, that is, with file handles that you open or acquire in the with statement.


On UNIX you can check .fileno() - 0 stands for standard in, 1 for standard out and 2 for standard error; generally you shouldn't close these streams, as there is really no way to reopen them anymore.


file.name is not really 100 % proof approach, since many streams can have a name, even though they are not real files, and that special name can clash with an existing file on the disk:

>>> import os.path
>>> os.path.exists(sys.stdin.name)
True
>>> sys.stdin.name
'<stdin>'

(hint, just moments before I did touch '<stdin>')

Also, in UNIX it is customary to unlink temporary files right after opening them, so they do not exist any more by that name; on the other hand even an existing file may be renamed after it is opened, and thus does not exist by the original name.


If you want to find out if a standard stream is redirected to a file, you can do:

>>> import os
>>> import stat   
>>> stat.S_ISREG(os.fstat(sys.stdout.fileno()).st_mode)
False

S_ISREG returns True if the mode describes an ordinary file (here the output goes to terminal so it is False); likewise you can find out if any file is redirected to a terminal with isatty:

>>> os.isatty(sys.stdout.fileno())
True

这篇关于Python文件对象是一个真实的文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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