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.
Related
Here i tried nptdms module but giving error
tdms_file = TdmsFile.as_dataframe('file.tdms',time_index=False, absolute_time=False, scaled_data=True)
AttributeError: 'str' object has no attribute 'groups'
please anyone have idea to convert tdms file csv or text file ,
here i don't know what is inside the file
You need to use the filepath and read in the actual file. Right now you are trying to read in the string 'file.tdms' which is merely 9 letters -- so you get the 'str' object error. Find the actual filepath for the file.tdms and you should be fine. It could look something like this:
filepath = '/home/x/y/file.tdms'
tdms_file = TdmsFile.as_dataframe(filepath,time_index=False, absolute_time=False, scaled_data=True)
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!
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.
I've been trying to get this work but getting the error TypeError: a bytes-like object is required, not 'str'' after upgrading to python 3.
What am I doing wrong here? I tried r, wb+ and w learned from here, Confused by python file mode "w+"
my code:
with open(output_filename, 'wb') as f:
# write column names
f.write("stack,overflow,super,user\n")
writer = csv.writer(f)
Can anyone help with this? Thanks.
The difference between 'wb' and 'w' filemodes is that 'wb' directly reads the binary and 'w' reads it as string. Your issue is that you're using 'wb' instead of 'w'. csv.writer is expecting a string, not binary.
If you use with open(output_filename, 'w') as f: instead, it should work.
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