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

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

问题描述

我意识到我一直使用的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" with read() and write() methods (and many more).

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

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