AttributeError: 'file' object has no attribute 'encode' - python

Traceback (most recent call last):
File "E:\blahblahblah\emailsend.py", line 26, in <module>
msg.attach(MIMEText(file))
File "E:\blahblahblah\Python 2.7.11\lib\email\mime\text.py", line 30, in __init__
self.set_payload(_text, _charset)
File "E:\blahblahblah\Python 2.7.11\lib\email\message.py", line 226, in set_payload
self.set_charset(charset)
File "E:\blahblahblah\Python 2.7.11\lib\email\message.py", line 268, in set_charset
cte(self)
File "E:\blahblahblah\Python 2.7.11\lib\email\encoders.py", line 73, in encode_7or8bit
orig.encode('ascii')
AttributeError: 'file' object has no attribute 'encode'https://stackoverflow.com/questions/ask#
I've been looking this up a lot but I haven't found an answer.
The only important parts of the code is this:
file = open('newfile.txt')
msg.attach(MIMEText(file))
There are other parts but I've debugged it and I get the error at the 'msg.attach(MIMEText(file))' line.
Any help?

MIMEText takes the content of the file, not the file object.
msg.attach(MIMEText(open("newfile.txt").read()))

Related

Looping Functionality On Discord Bot Doesn't Work

Whenever I try to use this looping functionality, it gives me an error that has lines that are further than the end of my code. I am fairly new to python and coding in general, but I think this is part of a repository I downloaded. The error code looks like this
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/player.py", line 603, in run
self._do_run()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/player.py", line 590, in _do_run
data = self.source.read()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/player.py", line 543, in read
ret = self.original.read()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/player.py", line 228, in read
ret = self._stdout.read(OpusEncoder.FRAME_SIZE)
AttributeError: 'NoneType' object has no attribute 'read'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/player.py", line 616, in _call_after
self.after(error)
File "main.py", line 242, in play_next_song
raise VoiceError(str(error))
VoiceError: 'NoneType' object has no attribute 'read'
My Entire code block is here. I am trying to host this discord bot in repl.it, if that has anything to do with it.
Any help would be appreciated, Thanks!
The relevant part of the stacktrace is actually the last one:
File "main.py", line 242, in play_next_song
raise VoiceError(str(error))
VoiceError: 'NoneType' object has no attribute 'read'
You cannot use the same FFmpegPCMAudio object twice. You have the create a new one.

Parsing GFF files throws TypeError

I am trying to parse a gif file with Biopython, and am using the sample code from their website. This is the code:
from BCBio import GFF
in_file = "infile.gff"
in_handle = open(in_file)
for rec in GFF.parse(in_handle):
print(rec)
in_handle.close()
When I run the code I get the following error:
Traceback (most recent call last):
File "/Users/juliofdiaz/anaconda2/envs/python37/lib/python3.7/site-packages/Bio/SeqIO/Interfaces.py", line 47, in __init__
self.stream = open(source, "r" + mode)
TypeError: expected str, bytes or os.PathLike object, not FakeHandle
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "get_genes_dpt.py", line 37, in <module>
for rec in GFF.parse(in_handle):
File "/Users/juliofdiaz/anaconda2/envs/python37/lib/python3.7/site-packages/BCBio/GFF/GFFParser.py", line 746, in parse
target_lines):
File "/Users/juliofdiaz/anaconda2/envs/python37/lib/python3.7/site-packages/BCBio/GFF/GFFParser.py", line 322, in parse_in_parts
for results in self.parse_simple(gff_files, limit_info, target_lines):
File "/Users/juliofdiaz/anaconda2/envs/python37/lib/python3.7/site-packages/BCBio/GFF/GFFParser.py", line 343, in parse_simple
for results in self._gff_process(gff_files, limit_info, target_lines):
File "/Users/juliofdiaz/anaconda2/envs/python37/lib/python3.7/site-packages/BCBio/GFF/GFFParser.py", line 637, in _gff_process
for out in self._lines_to_out_info(line_gen, limit_info, target_lines):
File "/Users/juliofdiaz/anaconda2/envs/python37/lib/python3.7/site-packages/BCBio/GFF/GFFParser.py", line 699, in _lines_to_out_info
fasta_recs = self._parse_fasta(FakeHandle(line_iter))
File "/Users/juliofdiaz/anaconda2/envs/python37/lib/python3.7/site-packages/BCBio/GFF/GFFParser.py", line 560, in _parse_fasta
return list(SeqIO.parse(in_handle, "fasta"))
File "/Users/juliofdiaz/anaconda2/envs/python37/lib/python3.7/site-packages/Bio/SeqIO/__init__.py", line 607, in parse
return iterator_generator(handle)
File "/Users/juliofdiaz/anaconda2/envs/python37/lib/python3.7/site-packages/Bio/SeqIO/FastaIO.py", line 183, in __init__
super().__init__(source, mode="t", fmt="Fasta")
File "/Users/juliofdiaz/anaconda2/envs/python37/lib/python3.7/site-packages/Bio/SeqIO/Interfaces.py", line 51, in __init__
if source.read(0) != "":
TypeError: read() takes 1 positional argument but 2 were given
I am not sure how to fix the error as it seems I am passing a str and not a FakeHandle. I am running biopython 1.78 with conda.

bencode including some binary data

I am using bencode library and I am trying to encode and decode binary data:
>>> import bencode
>>> bencode.Bencoder.decode(bencode.Bencoder.encode({'x': b'xyz'}))
Traceback (most recent call last):
File "<input>", line 1, in <module>
bencode.Bencoder.decode(bencode.Bencoder.encode({'x': b'xyz'}))
File "/home/pt12lol/virtualenv-python3/lib/python3.5/site-packages/bencode/__init__.py", line 228, in decode
return _decode(becode_string)
File "/home/pt12lol/virtualenv-python3/lib/python3.5/site-packages/bencode/__init__.py", line 197, in _decode
data = _decode_item(pf, six.next(src))
File "/home/pt12lol/virtualenv-python3/lib/python3.5/site-packages/bencode/__init__.py", line 178, in _decode_item
tok = next()
File "/home/pt12lol/virtualenv-python3/lib/python3.5/site-packages/bencode/__init__.py", line 147, in _tokenizer
s = m.group(m.lastindex) #
http://stackoverflow.com/questions/22489243/re-in-python-lastindex-attribute
AttributeError: 'NoneType' object has no attribute 'group'
How can I manage it?

AttributeError: '_GzipStreamFile' object has no attribute '_checkReadable

I am trying to setup commonsearch and i am the point where backend imports alexa1m data to rocksdb, but it die with error( error below ).
Traceback (most recent call last):
File "urlserver/import.py", line 21, in <module>
ds.import_dump()
File "./urlserver/datasources/__init__.py", line 62, in import_dump
for i, row in self.iter_dump():
File "./urlserver/datasources/__init__.py", line 102, in iter_dump
f = self.open_dump()
File "./urlserver/datasources/__init__.py", line 144, in open_dump
return GzipStreamFile(f)
File "/cosr/back/venv/src/gzipstream/gzipstream/gzipstreamfile.py", line 62, in __init__
super(GzipStreamFile, self).__init__(self._gzipstream)
File "/usr/lib64/python2.6/io.py", line 921, in __init__
raw._checkReadable()
AttributeError: '_GzipStreamFile' object has no attribute '_checkReadable'
have been fighting with this without progress for 2 days now.. if somebody could givme somekind of insight or advice i would be more than happy!
Found the solution, it was problem in the data parsed to the tool, not the tool itself.

AttributeError: 'module' object has no attribute 'generate_barcode'

I was trying to retrieve some data from DB in Python.and I got this error:
Traceback:
Traceback (innermost last):
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/app.py", line 49, in application
response = frappe.handler.handle()
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/handler.py", line 66, in handle
execute_cmd(cmd)
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/handler.py", line 77, in execute_cmd
method = get_attr(cmd)
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/handler.py", line 98, in get_attr
method = frappe.get_attr(cmd)
File "/home/adminuser/frappe-bench- hitech/apps/frappe/frappe/__init__.py", line 519, in get_attr
return getattr(get_module(modulename), methodname)
AttributeError: 'module' object has no attribute 'generate_barcode'
here is what I was doing:
#frappe.whitelist()
def generate_barcode(self):
brand=self.get("brand_code")
final_barcode="%05d" % random.randint(0,9999)
return {'final_barcode':final_barcode,'brand':brand}
can anyone help me?
The whitespace before #frappe.whitelist() could be the problem.

Categories