Is there a way to get more specific Error messages in Python? E.g The full error code or at least the line the error occurred on, the exact file that cannot be found rather than a generic "The system cannot find the file specified")
for file in ['C:/AA/HA.csv', 'C:/AA1/HA1.csv']:
try:
os.remove(file)
except OSError as e:
pass
print(getattr(e, 'message', repr(e)))
#print(getattr(e, 'message', repr(e)))
#print(e.message)
#print('File Not Removed')
The following prints twice:
FileNotFoundError(2, 'The system cannot find the file specified')
While this is great, is there a way to get more precise error messages for bug fixing?
The following stops the job but gives out in console the exact line being 855 as well as the file directory ''C:/AC/HA.csv''.
os.remove('C:/AA/HA.csv')
Traceback (most recent call last):
File "C:/ACA.py", line 855, in <module>
os.remove('C:/AC/HA.csv')
FileNotFoundError: [WinError 2] The system cannot find the file specified: ''C:/AC/HA.csv''
See the traceback module:
import os
import traceback
for file in ['C:/AA/HA.csv', 'C:/AA1/HA1.csv']:
try:
os.remove(file)
except OSError as e:
traceback.print_exc()
Output:
Traceback (most recent call last):
File "C:\test.py", line 6, in <module>
os.remove(file)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:/AA/HA.csv'
Traceback (most recent call last):
File "C:\test.py", line 6, in <module>
os.remove(file)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:/AA1/HA1.csv'
Related
So, I was making a game using Python 3 and the Ursina game engine and keep getting this error while trying to load the file:
Traceback (most recent call last):
File "main.py", line 5, in <module>
from src.menu import *
File "/home/mysteriousk/Desktop/moni moni moni/src/menu.py", line 4, in <module>
with open('../assets/database/database.json','r') as E:
FileNotFoundError: [Errno 2] No such file or directory: '../assets/database/database.json'
The file I am executing is main.py and the file I am importing is in the folder src and is named menu.py`. The code for loading my file:
with open('../assets/database/database.json','r') as E:
stuffs = json.load(E)
The file obviously exists in the directory assets/database. Please let me know whats wrong any help is appreciated.
Im trying to do a simple try except, and it is working. But I want to add some custom string at the beginning of the error message. If I just add it in print, its giving error.
import sys
try:
with open('./datatype-mapping/file.json') as rs_mapping:
data_mapping = json.load(rs_mapping)
except Exception as error:
print('CUSTOM ERROR: '+error)
sys.exit(1)
The error I got is,
Traceback (most recent call last):
File "c:/Users/rbhuv/Desktop/code/bqshift.py", line 22, in get_datatype_mapping
with open('./datatype-mapping/file.json') as rs_mapping:
FileNotFoundError: [Errno 2] No such file or directory: './datatype-mapping/file.json'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/rbhuv/Desktop/code/bqshift.py", line 102, in <module>
main()
File "c:/Users/rbhuv/Desktop/code/bqshift.py", line 99, in main
target_mapping()
File "c:/Users/rbhuv/Desktop/code/bqshift.py", line 39, in target_mapping
data_mapping = get_datatype_mapping()
File "c:/Users/rbhuv/Desktop/code/bqshift.py", line 26, in get_datatype_mapping
print('ERROR: '+error)
TypeError: can only concatenate str (not "FileNotFoundError") to str
But if I use just print(error) - this is working.
You need to convert error to str.
import sys
try:
int("fail")
except Exception as error:
print('CUSTOM ERROR: ' + str(error))
sys.exit(1)
This works flawlessly.
I am trying to set up the ChirpSDK, but every time I configure and run the code, I get this error:
Traceback (most recent call last):
File "test.py", line 3, in <module>
chirp = ChirpSDK()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/chirpsdk/chirpsdk.py", line 395, in __init__
self.read_chirprc(block)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/chirpsdk/chirpsdk.py", line 501, in read_chirprc
raise IOError('Could not find a ~/.chirprc file')
OSError: Could not find a ~/.chirprc file
Exception ignored in: <function ChirpSDK.__del__ at 0x10fa31af0>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/chirpsdk/chirpsdk.py", line 422, in __del__
self.close()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/chirpsdk/chirpsdk.py", line 470, in close
if self._sdk:
AttributeError: 'ChirpSDK' object has no attribute '_sdk'
I realize that the error is saying that my .chirprc file is not being recognized, but I have no idea how to remedy this. I created a .chirprc file in my /Users/username/ path, and named it c.chirprc (as the Chirp getting started article suggests), but I am still getting this error. Is there another part that I am missing? Am I reading the instructions wrong?
Thanks
The Chirp configuration file should be placed at /Users/<username>/.chirprc on macOS.
If you run ls -l ~/.chirprc in the terminal, do you get any results? If it displays no such file or directory then you have not created the file correctly.
I'm new to Python and self-teaching myself neural networks from this http://neuralnetworksanddeeplearning.com/chap1.html and I'm having trouble importing the files that I've downloaded for the exercises. This is the error message I keep getting:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
training_data, validation_data, test_data = mnist_loader.load_data_wrapper()
File "mnist_loader.py", line 68, in load_data_wrapper
File "mnist_loader.py", line 42, in load_data
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/gzip.py", line 34, in open
return GzipFile(filename, mode, compresslevel)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/gzip.py", line 94, in __init__
fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
IOError: [Errno 2] No such file or directory: '../data/mnist.pkl.gz'
I've looked around and tried this:
>>> open('Users/bryanjordan/Documents/neural-networks-and-deep-learning-master/mnist.pkl')
but get this error:
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
open('Users/bryanjordan/Documents/neural-networks-and-deep-learning-master/mnist.pkl')
IOError: [Errno 2] No such file or directory: 'Users/bryanjordan/Documents/neural-networks-and-deep-learning-master/mnist.pkl'
I also meet this question. And I find the solution to solve this problem.
If you run the code in the python console, please first use "cd" into the "/src" folder, and run the code.
If you run the code in a python file (like me), you should put the file into the "/src" folder.
It will be solved. The only reason is that the relative path. You should stand in the right place.
Hope to help you.
I wrote the following two pieces of code:
The first one is called_Process.py
print("Hello Python!!")
closeInput = raw_input("Press Enter to exit")
print "Closing calledProcess"
The second program is calling_Process.py which calls the above program:
import os
import sys
program = "python"
print("Process Calling")
arguments = ["called_Process.py"]
os.execvp(program, (program,) + tuple(arguments))
print("Finished calling called_Process!!")
When I run calling_Process.py on Windows using python 3.6.0, I get the following error:
Process Calling
Traceback (most recent call last):
File "C:/Users/ND/Desktop/calling_Process.py", line 14, in <module>
os.execvp(program, (program,) + tuple(arguments))
File "C:\Users\ND\AppData\Local\Programs\Python\Python36\lib\os.py", line 623, in execvp
_execvpe(file, args)
File "C:\Users\ND\AppData\Local\Programs\Python\Python36\lib\os.py", line 668, in _execvpe
raise last_exc.with_traceback(tb)
File "C:\Users\ND\AppData\Local\Programs\Python\Python36\lib\os.py", line 658, in _execvpe
exec_func(fullname, *argrest)
FileNotFoundError: [Errno 2] No such file or directory
>>> os.environ()
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
os.environ()
TypeError: '_Environ' object is not callable
Can someone help me understand what is causing this error?