Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there documentation for file object?

This is probably really dumb question, but I honestly can't find documentation for file object's API in Python 3.

Python docs for things using or returning file objects like open or sys.stdin have links to glossary with high-level introduction. It doesn't list functions exposed by such objects and I don't know, what can I do with them. I've tried googling for file object docs, but search engines don't seem to understand, what am I looking for.

I'm new to Python, but not to programming in general. Until now my scheme of using objects was to find complete API reference, see what it can do and then pick methods to use in my code. Is this wrong mindset in Python world? What are the alternatives?

like image 731
CodeSandwich Avatar asked Apr 17 '18 21:04

CodeSandwich


People also ask

What is the use of a file object?

A file object allows us to use, access and manipulate all the user accessible files. One can read and write any such files. When a file operation fails for an I/O-related reason, the exception IOError is raised.

What is a file object in Python?

File Objects ¶. File Objects. ¶. These APIs are a minimal emulation of the Python 2 C API for built-in file objects, which used to rely on the buffered I/O ( FILE*) support from the C standard library. In Python 3, files and streams use the new io module, which defines several layers over the low-level unbuffered I/O of the operating system.

How do I know if an object exists in a file?

If the object is an integer, its value is returned. If not, the object’s fileno () method is called if it exists; the method must return an integer, which is returned as the file descriptor value.

What are some examples of accessing a file?

Examples of accessing a file: A file can be opened with a built-in function called open (). This function takes in the file’s address and the access_mode and returns a file object.


1 Answers

open returns a file object that differs depending on the mode. From the open docs:

The type of file object returned by the open() function depends on the mode. When open() is used to open a file in a text mode ('w', 'r', 'wt', 'rt', etc.), it returns a subclass of io.TextIOBase (specifically io.TextIOWrapper). When used to open a file in a binary mode with buffering, the returned class is a subclass of io.BufferedIOBase. The exact class varies: in read binary mode, it returns an io.BufferedReader; in write binary and append binary modes, it returns an io.BufferedWriter, and in read/write mode, it returns an io.BufferedRandom. When buffering is disabled, the raw stream, a subclass of io.RawIOBase, io.FileIO, is returned.

Since it varies, open a file object with the mode you want help for and ask it for help:

>>> f = open('xx','w')
>>> help(f)

Help on TextIOWrapper object:

class TextIOWrapper(_TextIOBase)
 |  Character and line based layer over a BufferedIOBase object, buffer.
 |
 : etc...
like image 114
Mark Tolonen Avatar answered Nov 09 '22 00:11

Mark Tolonen