File-Like Objects in Python
Python supports file like objects, that don’t write to the disk but stay in the memory. You can create file like objects with StringIO. From Python version > 3 this is part of the io module. These files live only inside the computer memory, not on the disk. Python can read files from the disk, but this article focuses on files in memory.
StringIO
To start using file-like objects, first import the io module. Then create a new file with io.StringIO() where the parameter is the file contents.
>>> import io >>> >>> myFile = io.StringIO()
>>> myFile = io.StringIO("Data into the file") >>> myFile.read() 'Data into the file'
>>> myFile.seek(0) 0 >>> myFile.read() 'Data into the file' >>>
>>> import io >>> myFile = io.StringIO("Feeling good") >>> data = myFile.read() >>> print(data) Feeling good >>>
Write file
You can write data into the memory file too, by using the .write() method. This method is part of the object and as parameter takes a string (there’s also regular write file) The .write() method lets you write any data into the file. The usual escape character work \n for a new line.
>>> myFile = io.StringIO("") >>> myFile.write("Write a line into the file\n") >>> myFile.write("Second line.\n")
>>> data = myFile.getvalue() >>> data 'Write a line into the file\nSecond line.\n'
What is exactly a file-like object in Python?
File-like objects are mainly StringIO objects, connected sockets and, well, actual file objects.
If everything goes well, urllib.urlopen() returns a file-like object supporting the necessary methods.
Similar question
simplejson has the calls loads and dumps that consumes and produce strings instead of file like objects.
This link has an example in the context of StringIO and simplejson for both file-like and string objects.
This is the API for all file-like objects in Python (as of 3.10.5).
# All file-like objects inherit the IOBase interface: # Documented at https://docs.python.org/3/library/io.html#io.IOBase . close() -> None closed() -> bool # Implemented as @property `closed` fileno() -> int flush() -> None isatty() -> bool readable() -> bool readline(size: int = -1) -> Union[str, bytes] readlines(hint: Union[int, None] = None) -> list seek(pos: int, whence: int = io.SEEK_SET) -> int # SEEK_SET is 0 seekable() -> bool tell() -> int truncate(pos: int = None) -> int # The parameter is named "size" in class FileIO writable() -> bool writelines(lines: list) -> None __del__() -> None # Documented at https://docs.python.org/3/library/io.html#class-hierarchy . __enter__() __exit__(*args) -> None: __iter__() __next__() -> Union[str, bytes] # Documented in paragraph at https://docs.python.org/3/library/io.html#io.IOBase . # Note that while the documentation claims that the method signatures # of `read` and `write` vary, all file-like objects included in the Python # Standard Library have the following exact method signatures for `read` and `write`: read(size: int = -1) -> Union[str, bytes] write(b: Union[str, bytes]) -> int # The parameter is named "s" in TextIOBase
Specific file-like objects may implement more than this, but this is the subset of methods that are common to ALL file-like objects.
The IO Class Hierarchy section in the IO documentation contains a table listing the built-in and stub methods for the different types of file-like objects.
Basically, there is a hierarchy of abstract base classes:
- IOBase , which promises little
- RawIOBase , which provides unbuffered binary IO
- BufferedIOBase , which provides buffered binary IO
- TextIOBase , which provides buffered string IO
To implement a file-like object, you would subclass one of the three descendants of IOBase , but not IOBase itself. See this answer for attempting to determine which of these a given file-like object is.
Each of these classes provides various stub methods and mixins:
Class Stub Methods Mixins IOBase fileno , seek , truncate close , closed , __enter__ , __exit__ , flush , isatty , __iter__ , __next__ , readable , readline , readlines , seekable , tell , writable , writelines RawIOBase readinto , write read , readall BufferedIOBase detach , read , read1 , write readinto , readinto1 TextIOBase detach , read , readline , write encoding , errors , newlines The documentation for these methods can be found in the documentation for the classes, linked above.
Phoenix 1373
In Python, a file object is an object exposing an API having methods for performing operations typically done on files, such as read() or write() .
In the question’s example: simplejson.load(fp, . ) , the object passed as fp is only required to have a read() method, callable in the same way as a read() on a file (i.e. accepting an optional parameter size and returning either a str or a bytes object).
This does not need to be a real file, though, as long as it has a read() method.
A file-like object is just a synonym for file-object. See Python Glossary.
Adrian W 4213
file object
An object exposing a file-oriented API (with methods such as read() or write()) to an underlying resource. Depending on the way it was created, a file object can mediate access to a real on-disk file or to another type of storage or communication device (for example standard input/output, in-memory buffers, sockets, pipes, etc.). File objects are also called file-like objects or streams.
There are actually three categories of file objects: raw binary files, buffered binary files and text files. Their interfaces are defined in the io module. The canonical way to create a file object is by using the open() function.
Related Query
- In Python what type of object does *zip(list1, list2) return?
- File object in memory using Python
- What is the best way to create a Python object when you have the class implementaion stored in a string?
- What is the «m» behind python version like python3.6m
- copying input xml file and write exactly with Python
- How to access a block of memory as a file object in Python 2.5?
- What is the correct way to close a file opened inside object initialization?
- How can I package my python module and its dependencies such as Numpy/Scipy into one file like dll for C# calls?
- Python converting a text file into a json object
- Python convention for passing in a file path, file name, or file object
- What on earth. File permissions from files created by Python C code
- What is the best way to parse python script file in C/C++ code
- What is the use of strings like «-*- Mode: Python -*-» found at the top of some python files?
- what happens to a python object when you throw an exception from it
- Python rtree nearest — what exactly it does?
- Write Python object attributes to txt file
- Python import modules: what is different between a file and a directory?
- does C has anything like python pickle for object serialisation?
- How do I compare two files and output what is the same in a new file with python
- Python use regex match object in if statement and then access capture groups like Perl
- Does Python have a standard error log file and error_log function like PHP?
- Is there a way to add Julia, R and python to a single text file like R markdown or a notebook that could be manipulated as a text file?
- Object Creation in Python — What happenes to the modified variable?
- What is the most efficient way to read a large binary file python
- output an object to a file using python
- Python object attribute is another object, won’t load using pickle.load(). What am I doing wrong?
- Difference between a python dictionary object and what json.loads return
- Write a JSON object back to .json file with order in Python
- Python Installation: What is file path to python.exe?
- in python function have arguments object like javascript function arguments?
- python TfidfVectorizer gives typeError: expected string or bytes-like object on csv file
- Python for loop — what does it do exactly
- what can i do to let python use complement code to interpret bits just like c do
- Create an object from a JSON file in python (using a classmethod)?
- Python creating an Object while reading file
- How can I open a .NET FileStream object from a Python file handle?
- write python object __str__ to file
- Python os.access() reads file no matter what
- what is the way to replace a line in a file using python
- Python (3) readline returns empty string regardless of what is in the file
More Query from same tag
- Python: Set permissions for pip or conda in /opt directory
- TypeError: cannot concatenate ‘str’ and ‘function’ objects python files
- How can I display a matplotlib interactive window without any of the controls visible?
- How is this «operation» called in Python
- Implementing QAbstractProxyModel methods
- Create a list composed of one string per regex substitution?
- Write list of nested dictionaries to excel file in python
- Removing items from list if is not in ‘
- Color between the x axis and the graph in PyPlot
- Read between lines in text file
- How to convert data in particular html tag to dictionary
- why in python giving to str func a unicode string will throw an exception?
- Get function annotations in the ‘readable’ format
- plotnine/ggplot — changing legend positions
- Python: Queue.get() from multiple threads (or signal)
- Importing names from an aliased module. Is it possible?
- How to extend a line segment in both directions
- How do I get the name of a pytest mark for a test function?
- When does Python fall back onto class __dict__ from instance __dict__?
- Saving small numpy array as a large image
- Tkinter callback
- Matplotlib: Can we draw a histogram and a box plot on a same chart?
- How to write a function that finds the closest value in a list of integers?
- Input html form data from python script
- Filter pandas dataframe on dates and wrong format
- Repeat function a couple times without for loop in python
- Opening File Using a Variable that Contains File Name Python
- How to convert to bytes when TypeError: can’t concat bytes to str
- Edit a line in json lines / nd json
- Invalid credentials — 401 when trying to access Google Datastore from a GCE instance
- aiohttp Websocket doesn’t close when connected to some websocket servers
- QtWebkit crashes Python on Windows
- AWS IAM Python Boto3 script — Create User
- set constant width to every bar in a bar plot
- Does Python have a conditional NOT statement