Python script - file operations - python

I am trying to fetch the free disk space details of each drive using Python Scripts and storing the values in a variable 'p'.
p=(disk_usage('C:/'))
f=open('C:\\Python27\\solr.log','a')
f.write("Solr "+p)
I would like to place the results from variable 'p' into a log file. If I print the variable 'p', I am able to get the results successfully. But when I try to open a log file and write into it, it is throwing the below error.
"Traceback (most recent call last):
File "C:\Python27\sample.py", line 51, in <module>
f.write("Result: "+p)
TypeError: must be str, not UsageTuple"
Could anyone please help me on this.

I got the answer. Just changed the 'p' variable to string and then I tried to write into file. I am able to get the results in my log file.

Related

Script .py reading fine but not after building with pyinstaller

When i run setup.py as a script i have no issues reading a parameter file.
When i build with pyinstaller and run the same script as a .exe i receive the below error.
>setup.exe
Traceback (most recent call last):
File "setup.py", line 106, in <module>
param_file_info = paramsfx.extract_param_file_info(param_text)
File "app\paramsfx.py", line 64, in extract_param_file_info
s_n = re.search(rc_n, param_file_text)
File "c:\users\xxxxxx\appdata\local\programs\python\python37-32\lib\re.py", line 183, in search
return _compile(pattern, flags).search(string)
TypeError: cannot use a string pattern on a bytes-like object
[21776] Failed to execute script setup
i have read up on other posts how to resolve this error however before changing the code (that is working fine as a script) i wanted to see if anyone had any thoughts as to why it read the parameter differently as a .exe.
Just from looking at this output and not the code I noticed this:
"TypeError: cannot use a string pattern on a bytes-like object"
If this is true, I would recommend doing some python type conversion. Here is a link to help you out
https://www.w3schools.com/python/trypython.asp?filename=demo_numbers_convert
Because I don't know how your code is structured, choosing which form of conversion you choose may affect your results so please be careful with that.
I hope my explanation could have provided some use to you.

Unpickling error with MARK: how to solve it?

I am trying to load a simple list from a file to another, but python raises _pickle.UnpicklingError: could not find MARK as soon as I run it. The code is really simple and follows what the course tells me, I really don't understand. It goes as follows: file "donnees.py" has the list, and "fonctions.py" has the rest.
donnees.py
listemots=["bonjour","pivers","cactus","france","taureau","espace"]
fonctions.py
import pickle
import random
with open("donnees.py","rb") as donnees:
unpickler1=pickle.Unpickler(donnees)
listerecuperee=unpickler1.load()
print(listerecuperee)
Error raises is:
Traceback (most recent call last):
File "/Users/sebastienchabrol/Documents/Cours de python/pendu/fonctions.py", line 6, in <module>
listerecuperee=unpickler1.load()
_pickle.UnpicklingError: could not find MARK
Does someone have an idea about how to fix this ? Many thanks !!
For this just use:
from donnees import listemotes
Don’t use .py files to pickle variables. If you want to persist a list then use the csv module.

Python3 EOFError: EOF when reading a line Hackerrank, or any online portal

I always get this error whenever using input() in Python3 in any online compiler, hackerrank, wipro portal, interviewbit etc. I've seen so many posts regarding this, but none of them is working for me. try except block leads to always execution of the except block which I don't want as I'm still not able to read any input.
Even as simple as the following code doesn't work. Help.
b = int(input())
print (b)
I get the following error:
Traceback (most recent call last):
File "main.py", line 227, in
Z = obj.solve(A)
File "/tmp/judge/solution.py", line 9, in solve
b = int(input())
EOFError: EOF when reading a line
Try going to https://www.hackerrank.com/challenges/python-loops/problem where the single input line is already there in the starter code for you. If you select Python 3 from the language dropdown, and then -- without entering any code of your own at all -- click Run Code, you should get a Wrong Answer "no response on stdout" response. Do you get that, or do you still get an EOFError? I'm assuming you don't get the EOFError and that perhaps there's a problem with where/how you're entering your code into their editor.
If you're getting an error like that with InterviewBit, I'd say it's because with InterviewBit you're not supposed to be reading from stdin at all -- the starter code will have a function that their test cases call, and then you complete the function code to return the desired output.

DecodeError: Error parsing message for .binaryproto conversion to array

I'm currently trying to convert the .binaryproto file here to a usable numpy array. I'm running everything in my python terminal and following some of the guides as given here.
I can make it as far as seen below:
import caffe
blob = caffe.proto.caffe_pb2.BlobProto()
data = open('ucf101_train_mean.binaryproto','rb').read()
blob.ParseFromString(data)
At which point I get the error:
Traceback (most recent call last)L
File "<stdin>", line 1, in <module>
google.protobuf.message.DecodeError: Error parsing message
I've cleared and reinstalled caffe thinking it was an installation problem and it hasn't helped. I printed the data string and checked the length and both seem appropriate.
Or, as an alternative solution - is there another way I could potentially load the values of the .binaryproto file to get a usable mean? Thank you!

TypeError: expected a character buffer object while translating

I am working on a project that uses an sqlite3 database to store some of the data.
My search function uses the SQL statement: '''SELECT text FROM snippets WHERE title=?''', (whichName,) and as my code was, whichName came in as a dictionary, which garnered this error:
Traceback (most recent call last):
File "snippets.py", line 93, in <module>
main()
File "snippets.py", line 24, in main
get_value_from_name(response)
File "snippets.py", line 58, in get_value_from_name
(whichName,))
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
Thus, I figured I needed to pass it as a string, so I just did name = str(response) to convert it to a string, but here is where the problem began. It gave me this:
[u'TEST'] <--- What is returned by the conversion to a string
None <--- What is returned by the search function
when I converted the dictionary to a string. The search function then returned None because it was being passed [u'TEST'] instead of TEST like it should have been. So, I added some translation code:
translation_table = dict.fromkeys(map(ord, '(),'), None)
# Above code creates a table with the mapped characters
name = str(response)
name = name.translate(translation_table)
This is where my current problem is. It is returning the following error:
Traceback (most recent call last):
File "snippets.py", line 93, in <module>
main()
File "snippets.py", line 20, in main
name = name.translate(translation_table)
TypeError: expected a character buffer object
I looked at these questions:
Python TypeError: expected a character buffer object, personal misunderstanding - not applicable to my problem
expected buffer object error on string.translate - python 2.6 - also not applicable, as my translation table is created from a string not a dictionary
Getting error "expected character buffer object" and I don't know why - also not applicable because he is trying to do it at an index. I'm trying to do it like it should, finding/replacing throughout the entire string.
but none are applicable to my issue (as far as I can see.)
Does anyone know what is causing the Type Error?
Thanks!
I solved this. I ran the code in Python 3.3.2+ and it works find (albeit a few minor tweaks to the translation table)
Sorry if I wasted anyone's time. I will mark this as answer as soon as I can.

Categories

Resources