Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between os.open and os.fdopen in python

Tags:

python

I am really confused when to use os.open and when to use os.fdopen

I was doing all my work with os.open and it worked without any problem but I am not able to understand under what conditions we need file descriptors and all other functions like dup and fsync

Is the file object different from file descriptor

i mean f = os.open("file.txt",w)

Now is f the fileobject or its the filedescriptor?

like image 327
user1994660 Avatar asked Feb 23 '13 10:02

user1994660


People also ask

What is fdopen in python?

Python method fdopen() returns an open file object connected to the file descriptor fd. Then you can perform all the defined functions on file object.

What is the difference between open and with open in python?

Part 1: The Difference Between open and with open Basically, using with just ensures that you don't forget to close() the file, making it safer/preventing memory issues.

What does OS open return in Python?

open() method in Python is used to open a specified file path and set various flags according to the specified flags and its mode according to specified mode. This method returns a file descriptor for newly open file.

What is OS close in Python?

close() method in Python is used to close the given file descriptor, so that it no longer refers to any file or other resource and may be reused. A file descriptor is small integer value that corresponds to a file or other input/output resource, such as a pipe or network socket.


1 Answers

You are confusing the built-in open() function with os.open() provided by the os module. They are quite different; os.open(filename, "w") is not valid Python (os.open accepts integer flags as its second argument), open(filename, "w") is.

In short, open() creates new file objects, os.open() creates OS-level file descriptors, and os.fdopen() creates a file object out of a file descriptor.

File descriptors are a low-level facility for working with files directly provided by the operating system kernel. A file descriptor is a small integer that identifies the open file in a table of open files kept by the kernel for each process. A number of system calls accept file descriptors, but they are not convenient to work with, typically requiring fixed-width buffers, multiple retries in certain conditions, and manual error handling.

File objects are Python classes that wrap file descriptors to make working with files more convenient and less error-prone. They provide, for example, error-handling, buffering, line-by-line reading, charset conversions, and are closed when garbage collected.

To recapitulate:

  • Built-in open() takes a file name and returns a new Python file object. This is what you need in the majority of cases.

  • os.open() takes a file name and returns a new file descriptor. This file descriptor can be passed to other low-level functions, such as os.read() and os.write(), or to os.fdopen(), as described below. You only need this when writing code that depends on operating-system-dependent APIs, such as using the O_EXCL flag to open(2).

  • os.fdopen() takes an existing file descriptor — typically produced by Unix system calls such as pipe() or dup(), and builds a Python file object around it. Effectively it converts a file descriptor to a full file object, which is useful when interfacing with C code or with APIs that only create low-level file descriptors.

Built-in open can be emulated by combining os.open() (to create a file descriptor) and os.fdopen() (to wrap it in a file object):

# functionally equivalent to open(filename, "r") f = os.fdopen(os.open(filename, os.O_RDONLY)) 
like image 112
user4815162342 Avatar answered Sep 20 '22 06:09

user4815162342