python中的os.open和os.fdopen有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15039528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what is the difference between os.open and os.fdopen in python
提问by user1994660
I am really confused when to use os.openand when to use os.fdopen
我真的很困惑何时使用os.open以及何时使用os.fdopen
I was doing all my work with os.openand it worked without any problem but I am not able to understand under what conditions we need file descriptorsand all other functions like dupand fsync
我正在做我所有的工作os.open并且它没有任何问题,但我无法理解在什么条件下我们需要file descriptors以及所有其他功能,例如dup和fsync
Is the file objectdifferent from file descriptor
是file object不同于file descriptor
i mean f = os.open("file.txt",w)
我的意思是 f = os.open("file.txt",w)
Now is f the fileobject or its the filedescriptor?
现在 f 是文件对象还是它的文件描述符?
采纳答案by user4815162342
You are confusing the built-in open()function with os.open()provided by the osmodule. They are quite different; os.open(filename, "w")is not valid Python (os.openaccepts integer flags as its second argument), open(filename, "w")is.
您将内置open()功能与模块os.open()提供的功能混淆了os。它们完全不同;os.open(filename, "w")不是有效的 Python(os.open接受整数标志作为其第二个参数),open(filename, "w")是。
In short, open()creates new file objects, os.open()creates OS-level file descriptors, and os.fdopen()creates a file object out of a file descriptor.
简而言之,open()创建新的文件对象,os.open()创建操作系统级别的文件描述符,并os.fdopen()从文件描述符中创建文件对象。
File descriptors are a low-level facility for working with files directly provided by the operating system kernel. A file descriptor is a small integer that identifies the open file in a table of open files kept by the kernel for each process. A number of system calls accept file descriptors, but they are not convenient to work with, typically requiring fixed-width buffers, multiple retries in certain conditions, and manual error handling.
文件描述符是一种用于处理操作系统内核直接提供的文件的低级工具。文件描述符是一个小整数,用于标识内核为每个进程保存的打开文件表中的打开文件。许多系统调用接受文件描述符,但使用起来并不方便,通常需要固定宽度的缓冲区、在某些条件下多次重试以及手动错误处理。
File objects are Python classes that wrap file descriptors to make working with files more convenient and less error-prone. They provide, for example, error-handling, buffering, line-by-line reading, charset conversions, and are closed when garbage collected.
文件对象是包装文件描述符的 Python 类,使处理文件更方便且不易出错。例如,它们提供错误处理、缓冲、逐行读取、字符集转换,并在垃圾收集时关闭。
To recapitulate:
概括一下:
Built-in
open()takes a file name and returns a new Python file object. This is what you need in the majority of cases.os.open()takes a file name and returns a new file descriptor. This file descriptor can be passed to other low-level functions, such asos.read()andos.write(), or toos.fdopen(), as described below. You only need this when writing code that depends on operating-system-dependent APIs, such as using theO_EXCLflag toopen(2).os.fdopen()takes an existing file descriptor — typically produced by Unix system calls such aspipe()ordup(), and builds a Python file object around it. Effectively it converts a file descriptor to a full file object, which is useful when interfacing with C code or with APIs that only create low-level file descriptors.
内置
open()函数接受一个文件名并返回一个新的 Python 文件对象。在大多数情况下,这是您所需要的。os.open()接受一个文件名并返回一个新的文件描述符。可以将此文件描述符传递给其他低级函数,例如os.read()andos.write()或 toos.fdopen(),如下所述。只有在编写依赖于操作系统相关 API 的代码时才需要它,例如使用O_EXCL标志到open(2).os.fdopen()接受一个现有的文件描述符——通常由 Unix 系统调用产生,例如pipe()ordup(),并围绕它构建一个 Python 文件对象。它有效地将文件描述符转换为完整的文件对象,这在与 C 代码或仅创建低级文件描述符的 API 接口时非常有用。
Built-in opencan be implemented using os.open()(to create a file descriptor) and os.fdopen()(to wrap it in a file object):
open可以使用os.open()(创建文件描述符)和os.fdopen()(将其包装在文件对象中)来实现内置:
# equivalent to open(filename, "r")
f = os.fdopen(os.open(filename, os.O_RDONLY))

