Python 上的 io.open() 和 os.open() 有什么区别? [英] What's the difference between io.open() and os.open() on Python?

查看:30
本文介绍了Python 上的 io.open() 和 os.open() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到我一直在使用的 open() 函数是 io.open() 的别名,并且导入了 *from 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 接口.它将操作系统级文件描述符包装在一个对象中,您可以使用该对象以 Pythonic 方式访问文件.

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 系统调用的包装器.它需要更少的符号(和更多的 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() 文档:

From the os.open() documentation:

此函数用于低级 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" with read() and write() methods (and many more).

这篇关于Python 上的 io.open() 和 os.open() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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