Python上的io.open()和os.open()有什么区别? [英] What's the difference between io.open() and os.open() on Python?
问题描述
我意识到我一直使用的open()
函数是io.open()
的别名,并且从os
导入*
会使它黯然失色.
I realised that the open()
function I've been using was an alias to io.open()
and that importing *
from os
would overshadow that.
通过io
模块和os
模块打开文件有什么区别?
What's the difference between opening files through the io
module and os
module?
推荐答案
io.open()
是文件I/O的首选高级接口.它将操作系统级别的文件描述符包装在一个对象中,您可以使用该对象以Python方式访问文件.
io.open()
is the preferred, higher-level interface to file I/O. It wraps the OS-level file descriptor in an object that you can use to access the file in a Pythonic manner.
os.open()
只是较低级POSIX syscall的包装.它使用更少的符号(和更多POSIX-y)参数,并返回代表打开文件的文件描述符(一个数字).它不会不返回文件对象;返回的值将没有read()
或write()
方法.
os.open()
is just a wrapper for the lower-level POSIX syscall. It takes less symbolic (and more POSIX-y) arguments, and returns the file descriptor (a number) that represents the opened file. It does not return a file object; the returned value will not have read()
or write()
methods.
从 os.open()
文档:
此功能用于低级I/O.为了正常使用,请使用内置函数
open()
,该函数将使用read()
和write()
方法(以及更多方法)返回文件对象".
This function is intended for low-level I/O. For normal usage, use the built-in function
open()
, which returns a "file object" withread()
andwrite()
methods (and many more).
这篇关于Python上的io.open()和os.open()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!