expected str, bytes or os.PathLike object, not _io.TextIOWrapper - python-3.x

I was trying to convert a .txt file to .xml file for using tensorflow object detection API. Below is my code to write the result to a file:
with open(text_file,"w") as op:
op.write(str(class_num))
op.write(" ")
op.write(str(x_tl))
op.write(" ")
op.write(str(y_tl))
op.write(" ")
op.write(str(x_br))
op.write(" ")
op.write(str(y_br))
op.write("\n")
When i run this, I'm getting the following error:
TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper
Can someone help me.

Your error could be reproduced with the sample code mentioned below:
text_file = 'abc.txt'
text_file = open(text_file)
print(type(text_file))
with open(text_file,"a+") as op:
r = op.write('\n Gurudhevobhava')
The error is because of the the line, text_file = open(text_file), and as "neutrino_logic" rightly mentioned, print(type(text_file)) prints <class '_io.TextIOWrapper'>.
Error can be resolved by removing the line,text_file = open(text_file).
Sample working code is shown below:
text_file = 'abc.txt'
print(type(text_file))
with open(text_file,"a+") as op:
r = op.write('\n Gurudhevobhava')
Please let me know if you face any other error, I will be happy to help you.
Happy Learning!

Related

Getting the "bytes-like object is required" error, but the fix causes a "write() arguments must be str" error

This is a piece of a script that is giving me fits. If I use 'wb', I get the "bytes like" error. If I change it to 'w', I get the "write() arguments" error.
with open(output_path+filename, 'wb') as f:
f.write('<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n')
f.write(ET.tostring(elem, pretty_print = True))
If I have 'wb', it gives me this error:
f.write("<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n")
TypeError: a bytes-like object is required, not 'str'
If I have 'w', it gives me:
f.write(ET.tostring(elem, pretty_print = True))
TypeError: write() argument must be str, not bytes
The reason I'm doing it like this, is because I'm using lxml to take one HUGE XML sheet with several files'-worth of metadata and split it up into several XML files. So the like with the UTF-8 encoding is so that each individual file, has that written at the top. Without posting a ton of code, ET = lxml.etree.

Python 3 - correct way to write to .html without TypeError

# save-webpage.py (To write first 100 characters of html source into 'simple.html')
import urllib.request, io, sys
f = urllib.request.urlopen('https://news.google.com')
webContent = f.read(100)
#g = io.open('simple.html', 'w', encoding='UTF-8')
g = io.open('simple.html', 'w')
#g.write(webContent)
g.write(webContent.decode("UTF-8"))
g.close()
2019-01-11: See above for corrected working code after answers were received. Thanks guys.
Original question:
Upon execution, the file, simple.html, is created with 0 bytes.
Along with an error:
TypeError: must be str, not bytes.
Please help. I've gone about this several ways but to no avail. Thank you in advance!
g.write(webContent.decode("utf-8"))
File objects opened in text mode require you to write Unicode Text.
In this line you encoded to UTF-8 bytes
g = io.open('simple.html', 'w', encoding='UTF-8')
You could either not encode or try decoding it after.

Python3 open text file

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.

TypeError: string indices must be integers -python

I am using pubmed_lookup package in python, for some reasons, I got an error:
TypeError: string indices must be integers ;
and I cannot see what's wrong. I haven't got the foggiest idea about this error!   
with open ('file location','r') as f:
urls =[line.strip() for line in f]
lookup = [ ]
for url in urls:
lookup.append (PubMedLookup(url,email))
i = 0
while True:
publication = Publication(lookup[i])
print(
"""
JOURNAL:\n{journal}\n
"""
.format(**{
'journal':publication.journal,
}))
i +=1
if i >3:
break
My file format is as follows.
http://www.ncbi.nlm.nih.gov/pubmed/28483699
http://www.ncbi.nlm.nih.gov/pubmed/26318800
http://www.ncbi.nlm.nih.gov/pubmed/28766011
Thanks a lot!!!
The error statement might be this:
publication = Publication(lookup[i])

Writing exif data to the exif header using PyExifTool

Looking over a post from 2015 link on how to use PyExifTool to write to the Exif header. I gave it a try:
import exiftool
fileno=r'DSC00001.JPG
with exiftool.ExifTool() as et:
et.execute("EXIF:GPSLongitude=100",fileno)
et.execute("EXIF:GPSLatitude=100",fileno)
In response, I got the following error:
TypeError: sequence item 0: expected a bytes-like object, str found
Then as specified in the documentation, execute takes byte commands, so I bites, so I tried that too:
with exiftool.ExifTool() as et:
et.execute(bytes("EXIF:GPSLongitude=100", 'utf-8'),fileno)
et.execute(bytes("EXIF:GPSLatitude=50",'utf-8'),fileno)
But still got the same error :
TypeError: sequence item 1: expected a bytes-like object, str found
I am not sure what am I doing wrong, and if Exiftool can write to file.
The problem is that the execute method is low-level and requires bytes as inputs for both the parameters you pass and the file name. Try this:
import exiftool
pic = b"DSC00001.JPG"
with exiftool.ExifTool() as et:
et.execute(b"-GPSLatitude=11.1", pic)
tag = et.get_tag("EXIF:GPSLatitude", pic)
print(tag)
#Gracias!
exif = r'...\exiftool.exe'
file=br"...\FRM_20220111_134802.JPG"
with exiftool.ExifTool(exif) as et:
et.execute(b"-DateTimeOriginal=2022:10:10 10:10:10", file)
tag = et.get_tag("EXIF:DateTimeOriginal", file)
...
#RCM_Chile

Resources