Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a file in Python return a stream?

I have a simple qustion related opening file in Python.

Doing something like this:

x = open('test.txt', 'rt')
print(x)

I obtain this output:

<_io.TextIOWrapper name='test.txt' mode='rt' encoding='cp1252'>

that is the Python object representing the opened file. Is it considerable a stream or not? What exactly represents a stream in Python?

like image 600
AndreaNobili Avatar asked Oct 28 '25 08:10

AndreaNobili


1 Answers

According to official documents of python 3, it is a stream.

The easiest way to create a text stream is with open(), optionally specifying an encoding:

f = open("myfile.txt", "r", encoding="utf-8")

like image 67
SMMousaviSP Avatar answered Oct 31 '25 00:10

SMMousaviSP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!