Do not exit if the file is not found python3 - python

Hi I have a list of filenames
When reading a file, it comes out with an error
Traceback (most recent call last):
File "test.py", line 229, in <module>
f = open(i, 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'directory_home.json'
How can I skip the error and complete the rest of the files?
code
file = ["directory_home.json", "directory_ever.json", "home.json", "home.json"]
for i in file:
f = open(i, 'r')
data = json.load(f)
for i in data['name']:
print(i)

Something like this. Note that 'pass' goes to next iteration. 'Continue' would repeat iteration (in this case useless). 'Break' would end iteration.
file = ["directory_home.json", "directory_ever.json", "home.json", "home.json"]
for i in file:
try:
f = open(i, 'r')
data = json.load(f)
for i in data['name']:
print(i)
f.colse()
except FileNotFoundError:
print("File does not exist")
pass

Two options.
Option 1: os.path.isfile
import os
file = ["directory_home.json", "directory_ever.json", "home.json", "home.json"]
for i in file:
if os.path.isfile(file):
f = open(i)
data = json.load(f)
for i in data['name']:
print(i)
else:
continue # or whatever you want to do.
Option 2: Exception handling
file = ["directory_home.json", "directory_ever.json", "home.json", "home.json"]
for i in file:
try:
f = open(i, 'r')
except FileNotFoundError:
continue # or whatever
data = json.load(f)
for i in data['name']:
print(i)

You should pur your code in a try-except statement, like this
try:
file = ["directory_home.json", "directory_ever.json", "home.json", "home.json"]
for i in file:
f = open(i, 'r')
data = json.load(f)
for i in data['name']:
print(i)
except FileNotFoundError as e:
# log exception and do whatever you want
pass
In this way, if your code raises an exception because the file si not present, you are able ti handle It.
Please remember that in the code above, just the exception due to a not existing file si catch. This means that if another exception occurs the code will raise a traceback

Related

How to add a try & except construct so that the script ignores that there is no file

I don't really know the Python language, so I'm asking for help from experts. I have a simple script and I need to add a construct to it
try:
except:
this is necessary so that the script ignores that there is no 'file.txt' file and does not display an error.
If the file "file.txt" is missing, the script.py script displays the following error:
Version 1.2.1.
Traceback (most recent call last):
File "script.py", line 10, in <module>
with open("file.txt") as myfile, open("save.txt", 'a') as save_file:
FileNotFoundError: [Errno 2] No such file or directory: 'file.txt'
How can I make the script ignore that there is no 'file.txt' and not throw this errorTraceback (most recent call last) ?
Script code:
import sys
if __name__ == '__main__':
if '-v' in sys.argv:
print(f'Version 1.2.1.')
h = format(0x101101, 'x')[2:]
with open("file.txt") as myfile, open("save.txt", 'a') as save_file:
for line in myfile:
if h in line:
save_file.write("Number = " + line + "")
print("Number = " + line + "")
Help how to add a try & except construct to it?
Thanks in advance for your help!
Put try: and except: around the code, and use pass in the except: block to ignore the error
try:
with open("file.txt") as myfile, open("save.txt", 'a') as save_file:
for line in myfile:
if h in line:
save_file.write("Number = " + line + "")
print("Number = " + line + "")
except FileNotFoundError:
pass
You do the try: and then have the indented-block of code you want to try and if the error is raised, it'll go to that except: block of code and do whatever you want there.
try:
with open("file.txt") as myfile, open("save.txt", 'a') as save_file:
for line in myfile:
if h in line:
save_file.write("Number = " + line + "")
print("Number = " + line + "")
except FileNotFoundError:
print("The error was found!")
# or do whatever other code you want to do, maybe nothing (so pass)
# maybe let the user know somehow, maybe do something else.
try:
with open("file.txt") as myfile, open("save.txt", 'a') as save_file:
for line in myfile:
if h in line:
save_file.write("Number = " + line + "")
print("Number = " + line + "")
except NameError:
print("file doesn't exist")
finally:
print("regardless of the result of the try- and except blocks, this block will be executed")

Why catching an exception close the open file, with and without context?

I was working on an issue from a small script I have written. It's a hook between two application. The issue is one application have been updated and now it use yaml instead of json as config file.
Minimal exemple
import os
import yaml
import json
config = {
'version': "2.0.2",
'journals': {
"default": "/test/yaml/bhla"
},
'editor': os.getenv('VISUAL') or os.getenv('EDITOR') or "",
'encrypt': False,
'template': False,
'default_hour': 9,
'default_minute': 0,
'timeformat': "%Y-%m-%d %H:%M",
'tagsymbols': '#',
'highlight': True,
'linewrap': 79,
'indent_character': '|',
}
with open("jrnl.yaml", 'w') as f:
yaml.safe_dump(config, f, encoding='utf-8', allow_unicode=True, default_flow_style=False)
This will create a yaml file where you will have run the code.
The problem
I first wrote this simple patch to allow my hook to work with both (json and yaml).
JRNL_CONFIG_PATH = "jrnl.yaml"
with open(JRNL_CONFIG_PATH, "r") as f:
try:
JRNL_CONFIG = json.load(f)
except json.JSONDecodeError:
JRNL_CONFIG = yaml.load(f, Loader=yaml.FullLoader)
TAGS_SYMBOL = JRNL_CONFIG.get("tagsymbols", "#")
But as a big suprise, when error is catched, f is closed because JRNL_CONFIG will return None and produce this error :
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-141-bc1ef847563b> in <module>()
----> 1 JRNL_CONFIG.get("tagsymbols", "#")
AttributeError: 'NoneType' object has no attribute 'get'
Questions
Why catching an exception close the open file, with and without context?
What is the best method to catch the json error and still be able to parse the file as yaml ?
Tested
Can't pass the file by name, as the config file may not have extension (.json, .yaml)
This work but it's far from elegant ...
try:
f = open(JRNL_CONFIG_PATH, "r")
JRNL_CONFIG = json.load(f)
except json.JSONDecodeError:
f = open(JRNL_CONFIG_PATH, "r")
JRNL_CONFIG = yaml.load(f, Loader=yaml.FullLoader)
finally:
f.close()
Edit 1
Question 1 : Why catching an exception close the open file, with and without context?
Have been awser by #jedwards
Question 2 : What is the best method to catch the json error and still be able to parse the file as yaml ?
Have been awsered by #chepner
The issue isn't that the file is being closed (it's not), it's that the file pointer is no longer at the expected place (the beginning of the file) when you try to use your fallback:
with open("some.yaml") as f:
try:
print("before", f.tell())
data = json.load(f)
except json.JSONDecodeError:
print("after", f.tell())
print("is closed:", f.closed)
Here, the .tell() method returns the location of the file pointer.
One solution would be to reset the file pointer inside the except block:
with open("some.yaml") as f:
try:
JRNL_CONFIG = json.load(f)
except json.JSONDecodeError:
f.seek(0)
JRNL_CONFIG = yaml.load(f, Loader=yaml.FullLoader)
What about:
with open(JRNL_CONFIG_PATH, "r") as f:
data = f.read()
try:
JRNL_CONFIG = json.loads(data)
except json.JSONDecodeError:
JRNL_CONFIG = yaml.load(data, Loader=yaml.FullLoader)
TAGS_SYMBOL = JRNL_CONFIG.get("tagsymbols", "#")
There's no need to try both json.load and yaml.load, because YAML is a superset of JSON and yaml.load will parse anything json.load can.
JRNL_CONFIG_PATH = "jrnl.json"
with open(JRNL_CONFIG_PATH, "r") as f:
JRNL_CONFIG = yaml.load(f, Loader=yaml.FullLoader)
TAGS_SYMBOL = JRNL_CONFIG.get("tagsymbols", "#")

save error compile message in txt file in python3

Could you help me that when I compile my program and if I have an error how can I redirect this error message in text file, before compilation terminate?
I wrote this code, But my problem is i want when i have an error after that my COMP_ERR.txt file create and write error inside this file.
but in my code this create before
import urllib.request
import os
import tempfile
import sys
import fileinput
original_stderr = sys.stderr
f_error = open("COMP_ERR.txt", "w")
sys.stderr = f_error
try:
url = sys.argv[1]
path_snip_file = sys.argv[2]
#url = 'http://pages.di.unipi.it/corradini/Didattica/AP-17/PROG-ASS/03/ClassWithTest.java'
#path_snip_file = "C:/Users/user/AppData/Local/Programs/Python/Python36/snip1.java"
path_remote_file_inOurComp = "C:/Users/user/AppData/Local/Programs/Python/Python36/ClassWithTest.java"
path_remote_file_inOurCom = "C:/Users/user/AppData/Local/Programs/Python/Python36/ClassWithTest1.java"
gt_url = urllib.request.urlretrieve(url)
print("the URL is: ")
print(gt_url)
link = input("input the path of file: ")
"""
f = open(link, "r")
for line in f:
print(line, end='')
f.close()
"""
#--------------------]
#copy snipper java file inside remote file
#[--------------------
with open(path_remote_file_inOurComp, 'w') as modify_file:
with open (link, 'r') as r_file:
for line in r_file:
if " public static void main(" not in line:
modify_file.write(line)
else:
with open(path_snip_file, 'r') as snip_file:
for lines in snip_file:
modify_file.write(lines)
modify_file.write('\n'+line)
#-------------------------------------------------------------
#refactor
#-------------------------------------------------------------
with open(path_remote_file_inOurCom, 'w') as ft:
with open(path_remote_file_inOurComp, 'r') as file_n:
for line in file_n:
line = line.replace("System.out.println(", "System.out.println(insertLength(")
line = line.replace(";", ");")
ft.write(line)
except IndexError:
print("Not enough input! ! !")
sys.stderr = original_stderr
f_error.close()
Check if it is useful to you
import sys
try:
raise
except Exception as err:
**exc_type, exc_obj, exc_tb = sys.exc_info()** # this is to get error line number and description.
file_name = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] # to get File Name.
error_string="ERROR : Error Msg:{},File Name : {}, Line no : {}\n".format(err,file_name,exc_tb.tb_lineno))
file_log = open("error_log.log", "a")
file_log.write(error_string)
file_log.close()
logf = open("download.log", "w")
for download in download_list:
try:
# code to process download here
except Exception as e: # most generic exception you can catch
logf.write("Failed to download {0}: {1}\n".format(str(download), str(e)))
# optional: delete local version of failed download
finally:
# optional clean up code
pass
apart from this approach, you can use logging library to save each and every logs of your application.
Following is the method 2 of the problem of saving logs.
import logging
logging.basicConfig(filename='gunicon-server.log',level=logging.DEBUG,
format='[%(levelname)s]: [%(asctime)s] [%(message)s]', datefmt='%m/%d/%Y %I:%M:%S %p')
try:
# code to process download here
except Exception as e: # most generic exception you can catch
logging.error(str(e))
finally:
# optional clean up code
pass
import sys
sys.stderr = open('errorlog.txt', 'w')
# do whatever
sys.stderr.close()
sys.stderr = sys.__stderr__
see this for more details : https://ubuntuforums.org/showthread.php?t=849752

How to implement STDOUT and file write based on parameter input

I have the input file that looks like this (infile.txt):
a x
b y
c z
I want to implement a program that enable user to write to STDOUT or file depending on the command:
python mycode.py infile.txt outfile.txt
Will write to file.
And with this
python mycode.py infile.txt #2nd case
Will write to STDOUT.
I'm stuck with this code:
import sys
import csv
nof_args = len(sys.argv)
infile = sys.argv[1]
print nof_args
outfile = ''
if nof_args == 3:
outfile = sys.argv[2]
# for some reason infile is so large
# so we can't save it to data structure (e.g. list) for further processing
with open(infile, 'rU') as tsvfile:
tabreader = csv.reader(tsvfile, delimiter=' ')
with open(outfile, 'w') as file:
for line in tabreader:
outline = "__".join(line)
# and more processing
if nof_args == 3:
file.write(outline + "\n")
else:
print outline
file.close()
When using 2nd case it produces
Traceback (most recent call last):
File "test.py", line 18, in <module>
with open(outfile, 'w') as file:
IOError: [Errno 2] No such file or directory: ''
What's the better way to implement it?
You can try this:
import sys
if write_to_file:
out = open(file_name, 'w')
else:
out = sys.stdout
# or a one-liner:
# out = open(file_name, 'w') if write_to_file else sys.stdout
for stuff in data:
out.write(stuff)
out.flush() # cannot close stdout
# Python deals with open files automatically
You can also use this instead of out.flush():
try:
out.close()
except AttributeError:
pass
This looks a bit ugly to me, so, flush will be just well.

Bad file descriptor error

If I try executing the following code
f = file('test','rb')
fout = file('test.out','wb')
for i in range(10):
a = f.read(1)
fout.write(a)
f.close()
f = fout
f.seek(4)
print f.read(4)
Where 'test' is any arbitrary file, I get:
Traceback (most recent call last):
File "testbad.py", line 12, in <module>
print f.read(4)
IOError: [Errno 9] Bad file descriptor
If however, I change just the fout line to use a temporary file:
import tempfile
f = file('test','rb')
fout = tempfile.NamedTemporaryFile()
for i in range(10):
a = f.read(1)
fout.write(a)
f.close()
f = fout
f.seek(4)
print f.read(4)
There are no errors. Does anyone know why this is? I would have expected the first case to work, but I must be doing something wrong.
Thanks in advance for any help!
you've only opened the file fout for writing, not reading. To open for both use
fout = file('test.out','r+b')

Categories