11

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?

6
  • 2
    @primusa That's the C api docs, not pure Python docs. Commented Apr 17, 2018 at 21:46
  • There's the TextIOBase class for text files such as sys.stdin. True, I had to follow a few links from sys.stdin to get there. Commented Apr 17, 2018 at 21:49
  • And there is open Commented Apr 17, 2018 at 21:49
  • 1
    So TextIOBase is a complete definition of file object API? Does input/output tutorial describe all methods provided by file objects? I'm asking seriously. Commented Apr 17, 2018 at 21:57
  • 2
    No, as others have said, there is not a single class of file object, it depends on the open mode. To list the methods available for any object then do a dir(object-name) Commented Apr 17, 2018 at 21:59

1 Answer 1

14

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, which will list and describe the methods available for that type of object:

>>> f = open('xx', 'w')
>>> f
<_io.TextIOWrapper name='xx' mode='w' encoding='cp1252'>
>>> help(f)
Help on TextIOWrapper object:

class TextIOWrapper(_TextIOBase)
 |  Character and line based layer over a BufferedIOBase object, buffer.
 |
 : etc...

dir() will also list all the methods and attributes of an object, but without any parameters or helpful descriptions.

>>> dir(f)
['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 'reconfigure', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'write_through', 'writelines']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.