Fatal Python Error - Read stats from file - Exception doesnt work - python

My Python scripts crashes sometimes while reading the stats from a file (last edit time). Sometimes it works for days, sometimes it crashes every hour. I can’t reproduce the bug :(
I tried to solve it with try except but the script simply crashes
Fatal Python error: (pygame parachute) Segmentation Fault
Code:
try:
stat = os.stat(self.pathDB+""+fileName+".txt")
except OSError as e:
print("OS ERROR - : %s" % e)
return False
except Exception as e:
print("FATAL ERROR - TEST: %s" % e)
return False
Does anyone has an idea how to solve this?

Related

Unable to correct Python 2.7 exception syntax

Running some code with Python3 reports the below error:
$ python3 report.py --regions ap-southeast-2 --file file.csv
File "report.py", line 51
except Exception, e:
^
SyntaxError: invalid syntax
Research indicates that this deprecated syntax. I have found conflicting information on how I may fix this.
I have tried to engage python3 syntax, which I believe would be to switch
try:
f = file(filepath, 'wt')
except Exception, e:
f = None
sys.stderr.write ('Could not open file %s. reason: %s\n' % (filepath, e))
To:
try:
f = file(filepath, 'wt')
except:
f = None
sys.stderr.write ('Could not open file %s. reason: %s\n' % (filepath, e))
What happens then is that I get errors relating to the "e" being missing... so I'm unsure how best and easiest to resolve the syntax issues between the two versions. Can you help or advise? Thanks!
If you really want a method that is compatible for both Python2 and Python3,
Then try something like this:
import sys
try:
### your filepath code goes here or any other code
except Exception:
tb, err = sys.exc_info()[:2]
print(err)
Using exc_info() here is good as it provides you with a tuple of information on what the error and the traceback to that faulting code, specifically the (type, value, traceback). In this case, you are getting back the traceback and the error (value).

Python: copying file when destination has this file in use

I need to periodically update Online Excel file on SharePoint. The problem occures when this file is opened by user. In this case I am getting Windows error message as below:
Is it possible to handle such error in Python script? By handle I mean keep trying to save file after some time by using time.sleep function. I have tried with most common approach:
import shutil
try:
shutil.copy2('Track_Changes_Testing.xlsx', destination_on_sharepoint)
except Exception as err:
print(err)
But I only get Windows error message popping out
You could do this with time.sleep() and a while loop, like you requested. However be aware that this script will run forever if the file does continue to be used. I am not sure if that behavior is intended or would be ideal
import shutil
import time
import sys
while True:
print("Runned") #debugging
try:
shutil.copy2('Track_Changes_Testing.xlsx', destination_on_sharepoint)
break
except (OSError, IOError):
print("OSError or IOError")
# wair 5 min before trying again
time.sleep(300)
except shutil.Error as e:
print(f"Error while copying: {e}" )
# wair 5 min before trying again
time.sleep(300)
except:
print("Unexpected error:", sys.exc_info()[0])
# wair 5 min before trying again
time.sleep(300)

How to catch all exceptions in Try/Catch Block Python?

I am writing python code to install all the library packages required by my program in the linux environment.So the linux may contain python 2.7 or 2.6 or both so I have developed a try and except block codes that will install pip packages in linux. Try block code consists of python 2.7 version pip install and Catch block contains python 2.6 version pip install. My Problem is the peace of code is working fine, when i tried to install pandas in python 2.6 its getting me some errror. I want to catch that exception. Can you please tell me how to improve my try except blocks to catch that exception
required_libraries = ['pytz','requests','pandas']
try:
from subprocess import check_output
pip27_path = subprocess.check_output(['sudo','find','/','-name','pip2.7'])
lib_installs = [subprocess.call((['sudo',pip27_path.replace('\n',''),'install', i])) for i in required_libraries]
except:
p = subprocess.Popen(['sudo','find','/','-name','pip2.6'], stdout=subprocess.PIPE);pip26_path, err = p.communicate()
lib_installs = [subprocess.call((['sudo',pip26_path.replace('\n',''),'install', i])) for i in required_libraries]
You can catch several exceptions using one block. Let's use Exception and ArithmeticError for exceptions.
try:
# Do something
print(q)
# Catch exceptions
except (Exception, ArithmeticError) as e:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(e).__name__, e.args)
print (message)
If you need to catch several exceptions and handle each one on its own then you'd write an except statement for each one.
try:
# Do something
print(q)
# Catch exceptions
except Exception as e:
print (1)
except ArithmeticError as e:
print (2)
# Code to be executed if the try clause succeeded with no errors or no return/continue/break statement
else:
print (3)
You can also check if the exception is of type "MyCustomException" for example using if statements.
if isinstance(e, MyCustomException):
# Do something
print(1)
As for your problem, I suggest splitting the code into two functions.
install(required_libraries)
def install(required_libraries, version='pip2.7'):
# Perform installation
try:
from subprocess import check_output
pip27_path = subprocess.check_output(['sudo','find','/','-name', version])
lib_installs = [subprocess.call((['sudo',pip27_path.replace('\n',''),'install', i])) for i in required_libraries]
except Exception as e:
backup(required_libraries)
def backup(required_libraries, version='pip2.6'):
try:
p = subprocess.Popen(['sudo','find','/','-name',version]], stdout=subprocess.PIPE);pip26_path, err = p.communicate()
lib_installs = [subprocess.call((['sudo',pip26_path.replace('\n',''),'install', i])) for i in required_libraries]
except Exception as e:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(e).__name__, e.args)
print (message)
#Handle exception
Note: I didn't test this, I'm no expert as well so I hope I can help.
Useful links:
Built-in Exceptions
Errors and Exceptions
Compound statements

How to solve FTP Error 2 Not a Directory

I am using the below code for downloading files from a ftp server.But I am getting an error [Errno 2] No such file or directory:, but the file present in the server and I can able download it via terminal. Can anyone help me!!
import ftplib
import os
remotpath='folder/subfolder'
try:
ftpclient = ftplib.FTP('ftp.xxxx.com')
ftpclient.login('user', 'pass')
ftpclient.cwd(remotpath)
print "login succeessfull"
files = ftpclient.nlst()
for eachFile in files:
saveTo = os.path.join(remotpath,eachFile)
if (not os.path.exists(saveTo)):
try:
ftpclient.retrbinary('RETR ' + eachFile, open(saveTo, 'wb').write)
#logging.info('\tdownloaded ' + saveTo)
downloaded += 1
except BaseException as e:
print('\terror downloading inside first %s - %s' % (eachFile, e.__str__()))
except ftplib.error_perm:
print('\terror downloading inside second %s - %s' % (eachFile, ftplib.error_perm))
except Exception as e:
print e
Does the destination directory ./folder/subfolder exist?
If not you need to create it before downloading files. Either do so using your OS commands (mkdir), or in Python using os.makedirs() :
import os
try:
os.makedirs(remotpath)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
You can add it somewhere before the for loop.
On another issue, the order of you exception handlers means that all exceptions raised in the inner try block would be handled in the except BaseException statement. This means that ftplib.error_perm will be caught in that statement because BaseException is more general, and not in the ftplib.error_perm statement as you might expect.
You should reorder your except statements in order of increasing generality.

CalledProcessError causes 500 error on apache even though it is (or should be) caught

I have a strange problem with subprocess.CalledProcessError when running my Django-project on a production server running Apache:
My code (UPDATE: added catch-all-exception handling - behaviour is unchanged) is as follows:
try:
command_string = 'gcc -O0 -g3 -Wall -c -fmessage-length=0 ' + cfile + ' -o ' + ofile
compile_result = subprocess.check_output(command_string,stderr=subprocess.STDOUT,shell=True)
#logger.warning(compile_result)
if compile_result != "": #Dann gab es einen Fehler bzw. ein Compiler-Warning --> Abbruch!
self.ausgabe = u"Compile:\n"
self.ausgabe += unicode(compile_result, "utf-8")
return
except subprocess.CalledProcessError as e:
self.ausgabe = u"Compilierfehler (Returncode {0}):\n".format(e.returncode)
self.ausgabe += unicode(e.output, "utf-8")
logger.error("CPE" + unicode(e.returncode, "utf-8") + unicode(e.output, "utf-8"))
return #die weiteren Schritte müssen gar nicht erst ausgeführt werden...
except:
logger.error(str(sys.exc_info()))
self.ausgabe = u"Compilieren nicht erfolgreich. Fehler:\n" + unicode(sys.exc_info(), "utf-8")
return
This is all working as expected when I run it on my Windows development machine and the djange testserver. The exception is caught when the command execution fails, the error handling works as expected.
When I move the code to my production server (ubuntu, apache), I get an "Internal Server Error 500" when the command execution fails which is not the desired behaviour. The apache error.log is not very helpful since it does not show any error.
My configuration is:
Apache/2.2.22 (Ubuntu) PHP/5.4.9-4ubuntu2.3 mod_wsgi/3.4 Python/2.7.4
(Yes, I restarted apache and I'm sure I run on the same code).
Any ideas on this one?
If catch-all exception handling is not catching this, then either you are not running this code, or the exception in production happens outside of the try block. The 500 could even be due to an error completely outside python.
Introduce a deliberate error in this section of code and see if you see a change in behaviour in production. That at least lets you rule out the possibility of running stale code.
Thanks to Martijn Pieters's comments and questions, I found some errors which resutled in the strange behaviour:
The except subprocess.CalledProcessError as e: part contained some unicode-conversions which run fine on system a and caused problems on system b. Therefore, a UnicodeConversionError was raised in my exception handling and this caused the 500 error since it was not caught.
An other problem in the posted code is, that unicode(sys.exc_info(), "utf-8")is raising an exception as well, because sys.exc_info() returns a tuple which is correctly converted to a string when using the str()-method, but unicode() can only deal with strings and not with tuples. One work around which did the job for me is unicode(str(...)).
Thanks to everyone who spent time in solving this question!

Categories