Warm tip: This article is reproduced from serverfault.com, please click

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

发布于 2011-08-28 07:03:07

I realised that the open() function I've been using was an alias to io.open() and that importing * from os would overshadow that.

What's the difference between opening files through the io module and os module?

Questioner
Gio Borje
Viewed
0
cdhowie 2011-08-28 15:10:37

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() 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.

From the os.open() documentation:

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).