1

I am trying to open a simple text file using

dir_path = os.path.dirname(os.path.realpath(__file__))
F = open(dir_path+"\\sankey2.txt","r")

The path to the file is :C:\Users\David\Google Drive\Jobs\1.Efe\Transporte\0.MODEL

Inside the text file is the following text :

sankey2.txt
$SETGLOBAL REFERENCIA

Now if I run python it gives me the error :

TypeError: decoding to str: need a bytes-like object, _io.TextIOWrapper found

So I tried the following 2 solutions : Trying this

F = bytes(open(dir_path+"\\sankey2.txt","r"))

I get

TypeError: 'str' object cannot be interpreted as an integer

Trying this

F = open(dir_path+"\\sankey2.txt","rb")

I get

TypeError: decoding to str: need a bytes-like object, _io.BufferedReader found

It is not entirely clear to me how to solve this problem. Encoding to UTF-8 doesn't help either.

Thanks for your help.

David
  • 129
  • 1
  • 11
  • Possible duplicate of [python 3.5: TypeError: a bytes-like object is required, not 'str' when writing to a file](https://stackoverflow.com/questions/33054527/python-3-5-typeerror-a-bytes-like-object-is-required-not-str-when-writing-t) – Andriy Makukha Jul 10 '18 at 13:33
  • I don't think so, because I try to use regex – David Jul 10 '18 at 13:43

0 Answers0