简体   繁体   English

Python 上的 io.open() 和 os.open() 有什么区别?

[英]What's the difference between io.open() and os.open() on Python?

I realised that the open() function I've been using was an alias to io.open() and that importing * from os would overshadow that.我意识到我一直在使用的open()函数是io.open()的别名,而从os导入*会掩盖它。

What's the difference between opening files through the io module and os module?通过io模块和os模块打开文件有什么区别?

io.open() is the preferred, higher-level interface to file I/O. io.open()是首选的、更高级的文件 I/O 接口。 It wraps the OS-level file descriptor in an object that you can use to access the file in a Pythonic manner.它将操作系统级文件描述符包装在一个对象中,您可以使用该对象以 Pythonic 方式访问文件。

os.open() is just a wrapper for the lower-level POSIX syscall. os.open()只是低级 POSIX 系统调用的包装器。 It takes less symbolic (and more POSIX-y) arguments, and returns the file descriptor (a number) that represents the opened file.它需要更少的符号(和更多的 POSIX-y)参数,并返回代表打开文件的文件描述符(一个数字)。 It does not return a file object;返回一个文件对象; the returned value will not have read() or write() methods.返回的值将没有read()write()方法。

From the os.open() documentation :os.open()文档

This function is intended for low-level I/O.此函数用于低级 I/O。 For normal usage, use the built-in function open() , which returns a “file object” with read() and write() methods (and many more).对于正常使用,使用内置函数open() ,它返回一个带有read()write()方法(以及更多)的“文件对象”。

Absolutely everything:绝对的一切:

  • os.open() takes a filename as a string, the file mode as a bitwise mask of attributes, and an optional argument that describes the file permission bits, and returns a file descriptor as an integer. os.open()文件名作为字符串,文件模式作为属性的位掩码,以及描述文件权限位的可选参数,并以整数形式返回文件描述符。

  • io.open() takes a filename as a string or a file descriptor as an integer, the file mode as a string, and optional arguments that describe the file encoding, buffering used, how encoding errors and newlines are handled, and if the underlying FD is closed when the file is closed, and returns some descendant of io.IOBase . io.open()文件名作为字符串或文件描述符为一个整数,文件模式作为一个字符串,以及描述文件编码的可选参数,缓冲剂使用的,如何编码错误和换行处理,并且如果基础FD 在文件关闭时关闭,并返回io.IOBase一些后代。

os.open is very similar to open() from C in Unix . os.openUnix 中的 C 中的open()非常相似。 You're unlikely to want to use it unless you're doing something much more low-level.除非您正在做一些更底层的事情,否则您不太可能想要使用它。 It gives you an actual file descriptor (as in, a number, not an object).它为您提供了一个实际的文件描述符(例如,一个数字,而不是一个对象)。

io.open is your basic Python open() and what you want to use just about all the time. io.open是您的基本 Python open()并且您几乎一直都想使用它。

To add to the existing answers:要添加到现有答案:

I realised that the open() function I've been using was an alias to io.open()我意识到我一直在使用的 open() 函数是 io.open() 的别名

open() == io.open() in Python 3 only. open() == io.open()仅在 Python 3 中。 In Python 2 they are different.在 Python 2 中,它们是不同的。

While with open() in Python we can obtain an easy-to-use file object with handy read() and write() methods, on the OS level files are accessed using file descriptors (or file handles in Windows).虽然在 Python 中使用open()我们可以通过方便的read()write()方法获得易于使用的文件对象,但在操作系统级别上,文件是使用文件描述符(或 Windows 中的文件句柄)访问的。 Thus, os.open() should be used implicitly under the hood.因此, os.open()应该在os.open()隐式使用。 I haven't examined Python source code in this regard, but the documentation for the opener parameter, which was added for open() in Python 3.3, says:在这方面,我没有检查过 Python 源代码,但是在 Python 3.3 中为open()添加的opener参数的文档说:

A custom opener can be used by passing a callable as opener .可以通过将 callable 作为opener传递来使用自定义开启 The underlying file descriptor for the file object is then obtained by calling opener with ( file , flags ).然后通过使用 ( file , flags ) 调用opener来获取文件对象的底层文件描述符。 opener must return an open file descriptor (passing os.open as opener results in functionality similar to passing None ). opener必须返回一个打开的文件描述符(将os.open作为opener传递os.open产生类似于传递None功能)。

So os.open() is the default opener for open() , and we also have the ability to specify a custom wrapper around it if file flags or mode need to be changed.所以os.open()是默认开启者open()我们也必须指定周围有一个定制的包装,如果要更改文件标志或模式需求的能力。 See the documentation for open() for an example of a custom opener, which opens a file relative to a given directory.有关自定义打开程序的示例,请参阅open()文档,该程序打开与给定目录相关的文件。

In Python 2, the built-in open and io.open were different (io.open was newer and supported more things).在 Python 2 中,内置的 open 和 io.open 是不同的(io.open 更新并支持更多的东西)。 In Python 3, open and io.open are now the same thing (they got rid of the old built-in open), so you should always use open.在 Python 3 中,open 和 io.open 现在是一回事(它们摆脱了旧的内置 open),因此您应该始终使用 open。 Code that needs to be compatible with Python 2 and 3 might have a reason to use io.open.需要与 Python 2 和 3 兼容的代码可能有理由使用 io.open。

Below code to validate this.下面的代码来验证这一点。

import io
with io.open("../input/files.txt") as f:
    text = f.read().lower()

with open('../input/files.txt', encoding='utf-8') as f2:
    text2 = f2.read().lower()

print(type(f))
print(type(f2))
# <class '_io.TextIOWrapper'>
# <class '_io.TextIOWrapper'>

Database and system application developers usually use open instead of fopen as the former provides finer control on when, what and how the memory content should be written to its backing store (ie, file on disk).数据库和系统应用程序开发人员通常使用open而不是fopen因为前者可以更好地控制何时、什么以及如何将内存内容写入其后备存储(即磁盘上的文件)。

In Unix-like operating system, open is used to open regular file, socket end-point, device, pipe, etc. A positive file descriptor number is returned for every successful open function call.在类 Unix 操作系统中, open用于打开常规文件、套接字端点、设备、管道等。每次成功调用open函数都会返回一个正的文件描述符编号。 It provides a consistent API and framework to check for event notification, etc on a variety of these objects.它提供了一致的 API 和框架来检查各种这些对象上的事件通知等。

However, fopen is a standard C function and is normally used to open regular file and return a FILE data structure.但是, fopen是标准的 C 函数,通常用于打开常规文件并返回FILE数据结构。 fopen , actually, will call open eventually.实际上, fopen最终会调用open fopen is good enough for normal usage as developers do not need to worry when to flush or sync memory content to the disk and do not need event notification. fopen足以正常使用,因为开发人员无需担心何时将内存内容刷新或同步到磁盘,也不需要事件通知。

os.open() method opens the file file and set various flags according to flags and possibly its mode according to mode. os.open()方法打开文件文件并根据标志设置各种标志,并可能根据模式设置其模式。

The default mode is 0777 (octal), and the current unmask value is first masked out.默认模式为 0777(八进制),首先屏蔽掉当前的 unmask 值。

This method returns the file descriptor for the newly opened file.此方法返回新打开文件的文件描述符。

While,尽管,

io.open() method opens a file, in the mode specified in the string mode. io.open()方法以字符串模式中指定的模式打开一个文件。 It returns a new file handle, or, in case of errors, nil plus an error message.它返回一个新的文件句柄,或者,如果出现错误,则返回 nil 和一条错误消息。

Hope this helps希望这可以帮助

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

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