[ACCEPTED]-What's the difference between io.open() and os.open() on Python?-operating-system

Accepted answer
Score: 33

io.open() is the preferred, higher-level interface 13 to file I/O. It wraps the OS-level file 12 descriptor in an object that you can use 11 to access the file in a Pythonic manner.

os.open() is 10 just a wrapper for the lower-level POSIX 9 syscall. It takes less symbolic (and more 8 POSIX-y) arguments, and returns the file 7 descriptor (a number) that represents the 6 opened file. It does not return a file object; the 5 returned value will not have read() or write() methods.

From 4 the os.open() documentation:

This function is intended for low-level 3 I/O. For normal usage, use the built-in 2 function open(), which returns a “file object” with 1 read() and write() methods (and many more).

Score: 8

Absolutely everything:

  • os.open() takes a filename as a string, the 9 file mode as a bitwise mask of attributes, and an 8 optional argument that describes the file permission bits, and 7 returns a file descriptor as an integer.

  • io.open() takes 6 a filename as a string or a file descriptor as an integer, the 5 file mode as a string, and optional arguments that describe the file 4 encoding, buffering used, how encoding errors 3 and newlines are handled, and if the underlying 2 FD is closed when the file is closed, and 1 returns some descendant of io.IOBase.

Score: 4

os.open is very similar to open() from C in Unix. You're unlikely to 5 want to use it unless you're doing something 4 much more low-level. It gives you an actual 3 file descriptor (as in, a number, not an 2 object).

io.open is your basic Python open() and what 1 you want to use just about all the time.

Score: 4

To add to the existing answers:

I realised 22 that the open() function I've been using 21 was an alias to io.open()

open() == io.open() in Python 20 3 only. In Python 2 they are different.

While 19 with open() in Python we can obtain an easy-to-use 18 file object with handy read() and write() methods, on 17 the OS level files are accessed using file 16 descriptors (or file handles in Windows). Thus, os.open() should 15 be used implicitly under the hood. I haven't 14 examined Python source code in this regard, but 13 the documentation for the opener parameter, which 12 was added for open() in Python 3.3, says:

A custom 11 opener can be used by passing a callable 10 as opener. The underlying file descriptor for 9 the file object is then obtained by calling 8 opener with (file, flags). opener must return an open file descriptor 7 (passing os.open as opener results in functionality similar to 6 passing None).

So os.open() is the default opener for 5 open(), and we also have the ability to specify 4 a custom wrapper around it if file flags 3 or mode need to be changed. See the documentation for 2 open() for an example of a custom opener, which 1 opens a file relative to a given directory.

Score: 4

In Python 2, the built-in open and io.open 7 were different (io.open was newer and supported 6 more things). In Python 3, open and io.open 5 are now the same thing (they got rid of 4 the old built-in open), so you should always 3 use open. Code that needs to be compatible 2 with Python 2 and 3 might have a reason 1 to use io.open.

Below code to validate this.

import io
with io.open("../input/files.txt") as f:
    text = f.read().lower()

with open('../input/files.txt', encoding='utf-8') as f2:
    text2 = f2.read().lower()

print(type(f))
print(type(f2))
# <class '_io.TextIOWrapper'>
# <class '_io.TextIOWrapper'>
Score: 1

Database and system application developers 18 usually use open instead of fopen as the former provides 17 finer control on when, what and how the 16 memory content should be written to its 15 backing store (i.e., file on disk).

In Unix-like 14 operating system, open is used to open regular 13 file, socket end-point, device, pipe, etc. A 12 positive file descriptor number is returned 11 for every successful open function call. It 10 provides a consistent API and framework 9 to check for event notification, etc on 8 a variety of these objects.

However, fopen is 7 a standard C function and is normally used 6 to open regular file and return a FILE data 5 structure. fopen, actually, will call open eventually. fopen is 4 good enough for normal usage as developers 3 do not need to worry when to flush or sync 2 memory content to the disk and do not need 1 event notification.

Score: 0

os.open() method opens the file file and set various 9 flags according to flags and possibly its 8 mode according to mode.

The default mode 7 is 0777 (octal), and the current unmask 6 value is first masked out.

This method returns 5 the file descriptor for the newly opened 4 file.

While,

io.open() method opens a file, in the 3 mode specified in the string mode. It returns 2 a new file handle, or, in case of errors, nil 1 plus an error message.

Hope this helps

More Related questions