I receive an error
File.open(classname+'.txt','a')
AttributeError: '_io.TextIOWrapper' object has no attribute 'open'
while trying to open a file. I need to open the file and write to the file with the scores.
Here is the code
if Exists==False:
File.open(classname+'.txt','a')
File.write(name+','+surname+','+str(1)+','+str(score)+'/n')
else:
File=open(classname+'.txt','w')
linecount=len(filelines)
for i in range(0,linecount):
File.write(filelines[i])
it should be
File=open(classname+'.txt','a')
File.write(name+','+surname+','+str(1)+','+str(score)+'/n')
File.close()
The problem is that at the beginning you declare
File=open(classname+'.txt','r+')
and then you ask again to open File
File.open(classname+'.txt','a')
but File is already open(classname+'.txt','r+'). Just skip File.open(classname+'.txt','a') and it should work fine.
Related
I'm attempting to open a thumbnail of an image that was saved with django-versatileimagefield
https://django-versatileimagefield.readthedocs.io/en/latest/
file = instance.image.thumbnail['1920x1080'].open(mode='rb')
I'm getting the following error:
'SizedImageInstance' object has no attribute 'open'
I have success with the following line of code, but I want to open the smaller version as opposed to the original version
file = instance.image.open(mode='rb')
If it helps, the url of the image is instance.image.thumbnail['1920x1080'].url
Thanks!
I am updating the document in solr using pysolr python. I am taking one field data from solr document and after updating it i am ingesting it again using
solr.add(dict)
Basically i am ingesting a dictionary in solr again.
However i am getting this error :
File "/usr/local/lib/python2.7/dist-packages/pysolr.py", line 907, in add
el = self._build_doc(doc, boost=boost, fieldUpdates=fieldUpdates)
File "/usr/local/lib/python2.7/dist-packages/pysolr.py", line 822, in _build_doc
for key, value in doc.items():
AttributeError: 'unicode' object has no attribute 'items'
tried solr.add(res[dict]) instead of solr.add(dict) It worked.
I was working on a python program made to write out the IP addresses from an error log file. I got it coded but whenever I try it on my error log I get a
TypeError: 'NoneType' object is not subscriptable
I wrote the code under this in case anyone wants to see what I did specifically. If anyone knows my mistake that would be great.
import re
with open(r'C:\Users\Admin\Desktop/Test error file.txt') as fh:
file = fh.readlines()
pattern = re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
list=[]
for line in file:
lst.append(pattern.search(line)[0])
print(list)
pattern.search returns None if no match is found. To account for this try
for line in file:
match = pattern.search(line)
if match is not None:
lst.append(match[0])
else:
lst.append(None)
Here I appended a None value if there was no match. You can change the logic to do what you need if there is no match.
I'm new to python. I'm trying to unzip a file in my current script directory but I can't get this work...
zip = zipfile.ZipFile(os.getcwd() + '/Sample_APE_File.zip')
zip.extractall(os.getcwd())
Error: AttributeError: 'str' object has no attribute 'getcwd'
You assigned a string value to os, it is no longer bound to the module:
os = 'some string'
Rename that to something else.
I am trying to read image from url and save it to model. but i am getting this error message:
AttributeError: addinfourl instance has no attribute '__committed'
this is my code:
location = Location()
location.save()
image = urllib.urlopen(img_url)
location.locations_image.create(bild=image)
I guess, i am doing something wrong while reading an image file from url. can someone pls guide me? thanks a lot