0

I am new at Python and I am not to sure about data types. If I coded wordfile = open("Sentence.txt","w"). What data type would "wordfile" be?

Cœur
  • 37,241
  • 25
  • 195
  • 267
code
  • 11
  • 1
  • 2
    Try `type(wordfile)`. But that's a pretty meaningless bit of information. Why do you need to know this? – Morgan Thrapp Sep 21 '16 at 16:14
  • @MorganThrapp sometimes it's not about why, it's just because curiosity is human nature. Yes knowing that might not be useful, but at least he/she/code knows. – MooingRawr Sep 21 '16 at 16:16
  • It can be useful sometimes. An example would be a function argument that could have different data types, and depending on that the function treats them in a different way by doing for instance: `if isinstance(list, argument):` – edgarstack Sep 21 '16 at 16:19

1 Answers1

2

It's a Text IO Wrapper, i.e. handler for IO operations.

>>> wordfile = open('file.txt', 'w')
>>> wordfile
<_io.TextIOWrapper name='file.txt' mode='w' encoding='cp1255'>
>>> type(wordfile)
<class '_io.TextIOWrapper'>

open and this class are contained in the io module, but can be accessed without import io. You can, however, import the io module and use the io.openmethod directly.


As official documentation claims:

class io.TextIOWrapper

A buffered text stream over a BufferedIOBase binary stream.

means, TextIOWrapper uses BufferedIOBase binary stream as a 'channel' for a text stream, so it can handle text files.

class io.BufferedIOBase

Base class for binary streams that support some kind of buffering.

Community
  • 1
  • 1
Uriel
  • 15,579
  • 6
  • 25
  • 46