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
I'm writing a short piece of code in python to check the status code of a list of URLS. The steps are
1. read the URL's from a csv file.
2. Check request code
3. Write the status code request into the csv next to the checked URL
The first two steps I've managed to do but I'm stuck with writing the output of the requests into the same csv, next to the urls. Please help.
import urllib.request
import urllib.error
from multiprocessing import Pool
file = open('innovators.csv', 'r', encoding="ISO-8859-1")
urls = file.readlines()
def checkurl(url):
try:
conn = urllib.request.urlopen(url)
except urllib.error.HTTPError as e:
print('HTTPError: {}'.format(e.code) + ', ' + url)
except urllib.error.URLError as e:
print('URLError: {}'.format(e.reason) + ', ' + url)
else:
print('200' + ', ' + url)
if __name__ == "__main__":
p = Pool(processes=1)
result = p.map(checkurl, urls)
with open('innovators.csv', 'w') as f:
for line in file:
url = ''.join(line)
checkurl(urls + "," + checkurl)
The .readlines() operation leaves the file object at the end of file. When you attempt to loop through the lines of file again, without first rewinding it (file.seek(0)) or closing and opening it again (file.close() followed by opening again), there are no lines remaining. Always recommended to use with open(...) as file construct to ensure file is closed when operation is finished.
Additionally, there appears to be an error in your input to checkurl. You have added a list (urls) to a string (",") to a function (checkurl).
You probably meant for this section to read
with open('innovators.csv', 'w') as f:
for line in urls:
url = ''.join(line.replace('\n','')) # readlines leaves linefeed character at end of line
f.write(url + "," + checkurl(url))
The checkurl function should return what you are intending to place into the csv file. You are simply printing to standard output (screen). Thus, replace your checkurl with
def checkurl(url):
try:
conn = urllib.request.urlopen(url)
ret='0'
except urllib.error.HTTPError as e:
ret='HTTPError: {}'.format(e.code)
except urllib.error.URLError as e:
ret='URLError: {}'.format(e.reason)
else:
ret='200'
return ret
or something equivalent to your needs.
Save the status in a dict. and convert it to dataframe. Then simply send it to a csv file. str(code.getcode()) will return 200 if the url is connecting else it will return an exception, for which i assigned status as '000'. So your csv file will contain url,200 if URL is connecting and url,000 if URL is not connecting.
status_dict={}
for line in lines:
try:
code = urllib.request.urlopen(line)
status = str(code.getcode())
status_dict[line] = status
except:
status = "000"
status_dict[line] = status
df = pd.Dataframe(status_dict)
df.to_csv('filename.csv')
In book headfirstpython in chapter4 they have used the syntax
print(list_name, file= output_file_name)
For them it's working fine, but for me it's giving syntax error on file = output_file_name. The python version is same i.e. 3.
code:
import os
man = []
other = []
try:
data = open('sketch.txt')
for each_line in data:
try:
(role, line_spoken) = each_line.split(':', 1)
line_spoken = line_spoken.strip()
if role == 'Man':
man.append(line_spoken)
elif role == 'Other Man':
other.append(line_spoken)
except ValueError:
pass
data.close()
except IOError:
print('The datafile is missing!')
try:
man_file = open('man_data.txt', 'w')
other_file = open('other_data.txt', 'w')
print(man, file=man_file)
print(other, file=other_file)
except IOError:
print('File error.')
finally:
man_file.close()
other_file.close()
As per the help of print function indicates
file: a file-like object (stream); defaults to the current
sys.stdout.
So the input is not supposed to be file-name but rather a file-like object. If you want to write into (say) a text file, you need to first open it for writing and use the file handle.
f = open("output.txt",'w')
print(list_name, file=f)
This script is supposed to generate a text file of stock price values. I can't seem to either find the text file that is supposed to be generated from this script or have this script actually create the desired text file... I added a section of code to check if the file exists, but I keep getting the result that the text file is indeed not created. Please let me know what I can do to correct. When I run the code I do not get any errors. Thanks.
import urllib2
import time
import os
import sys
stockToPull = 'AAPL'
def pullData(stock):
try:
fileLine = stock+'.txt'
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv'
sourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(', ')
if len(splitLine)==6:
if 'values' not in eachLine:
saveFile = open(fileLine,'a')
lineToWrite = eachLine+'\n'
saveFile.write(lineToWrite)
print 'Pulled', stock
print 'sleeping'
if os.path.isfile(fileLine): # checks to see if text file created
print "file does exist"
else:
print "No such file"
time.sleep(5)
except Exception, e:
print 'main loop', str(e)
pullData(stockToPull)
You are splitting each row on the string ', ' (note the space). You should be splitting on the comma only:
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine)==6:
# etc
You would be better off opening the file once, writing each line to it, then closing the file when finished. You can use a with statement to do this:
with open(fileLine, 'w') as outfile:
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine) == 6 and 'values' not in eachLine:
outfile.write('%s\n' % eachLine)
outfile.close()
i think you need to check whether data is arriving or not using
from urllib2 import Request, urlopen, URLError, HTTPError
fileLine = stock+'.txt'
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv'
try:
response = urlopen(urlToVisit)
sourceCode = response.read()
<place your logic here>
except HTTPError as e:
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
except URLError as e:
print 'We failed to reach a server.'
print 'Reason: ', e.reason
your code works fine
output i got
['uri:/instrument/1.0/AAPL/chartdata;type=quote;range=1y/csv', 'ticker:aapl', 'Company-Name:Apple Inc.', 'Exchange-Name:NMS', 'unit:DAY', 'timestamp:', 'first-trade:19801212', 'last-trade:20140702', 'currency:USD', 'previous_close_price:59.7843', 'Date:20130703,20140702', 'labels:20130703,20130801,20130903,20131001,20131101,20131202,20140102,20140203,20140303,20140401,20140501,20140602,20140701', 'values:Date,close,high,low,open,volume', 'close:59.2929,94.2500', 'high:60.1429,95.0500', 'low:58.6257,93.5700', 'open:59.0857,94.7300', 'volume:28420900,266380800', '20140620,90.9100,92.5500,90.9000,91.8500,100813200', .....
'20140702,93.4800,94.0600,93.0900,93.8700,28420900', '']
['uri:/instrument/1.0/AAPL/chartdata;type=quote;range=1y/csv']
['ticker:aapl']
['Company-Name:Apple Inc.']
['Exchange-Name:NMS']
['unit:DAY']
['timestamp:']
['first-trade:19801212']
['last-trade:20140702']
['currency:USD']
['previous_close_price:59.7843']
['Date:20130703,20140702']
['labels:20130703,20130801,20130903,20131001,20131101,20131202,20140102,20140203,20140303,20140401,20140501,20140602,20140701']
['values:Date,close,high,low,open,volume']
['close:59.2929,94.2500']
['high:60.1429,95.0500']
['low:58.6257,93.5700']
['open:59.0857,94.7300']
['volume:28420900,266380800']
['20130703,60.1143,60.4257,59.6357,60.1229,60232200']
['20130705,59.6314,60.4700,59.3357,60.0557,68506200']
['20130708,59.2929,60.1429,58.6643,60.0157,74534600']
......
['20140630,92.9300,93.7300,92.0900,92.1000,49482300']
['20140701,93.5200,94.0700,93.1300,93.5200,38170200']
['20140702,93.4800,94.0600,93.0900,93.8700,28420900']
['']
Pulled AAPL
sleeping
file does exist
Your code is fine, but I don't think it is able to do what you want or you don't know what you want. You haven't observed your data correctly. I have made minor changes to your script. Please run this script now:
import urllib2
import time
import os
import sys
stockToPull = 'AAPL'
def pullData(stock):
try:
fileLine = stock+'.txt'
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv'
sourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(', ')
if len(splitLine)==6:
print 'Entering outer'
if 'values' not in eachLine:
print 'Entering innter'
saveFile = open(fileLine,'a')
lineToWrite = eachLine+'\n'
saveFile.write(lineToWrite)
print 'Pulled', stock
print 'sleeping'
if os.path.isfile(fileLine): # checks to see if text file created
print "file does exist"
else:
print "No such file"
time.sleep(5)
except Exception, e:
print 'main loop', str(e)
pullData(stockToPull)
If you'd notice, I have just put two print statements inside the if blocks that actually write to a file. On running the script, I noticed that the print statements are never executed. So there are no errors in your code to my knowledge but it doesn't seem to be doing what you want it to. So re-check your data again.
Lastly, to solve these kind of problems, you must use the pdb library which stands for Python Debugger and it is an amazingly helpful tool to debug your code without making a mess of it. Checkout this video from PyCon.
Hello! I have the following script:
import os
import stat
curDir = os.getcwd()
autorun_signature = [ "[Autorun]",
"Open=regsvr.exe",
"Shellexecute=regsvr.exe",
"Shell\Open\command=regsvr.exe",
"Shell=Open" ]
content = []
def read_signature(file_path):
try:
with open(file_path) as data:
for i in range(0,5):
content.append(data.readline())
except IOError as err:
print("File Error: "+ str(err))
read_signature(os.getcwd()+'/'+'autorun.inf')
if(content==autorun_signature):
print("Equal content")
else:
print("Not equal")
It prints not equal, then I tried this method:
import os
import stat
curDir = os.getcwd()
autorun_signature = "[Autorun]\nOpen=regsvr.exe\nShellexecute=regsvr.exe\nShell\Open\command=regsvr.exe\nShell=Open"
content = ""
def read_signature(file_path):
try:
with open(file_path) as data:
content = data.read()
except IOError as err:
print("File Error: "+ str(err))
read_signature(os.getcwd()+'/'+'autorun.inf')
if(content==autorun_signature):
print("Equal content")
else:
print("Not equal")
It also print not equal!
I want to store the content of autorun.inf file in script and every time i find such file I want to check its content if it is or not, I could not do, can anyone help me?
the content of autorun.inf:
[Autorun]
Open=regsvr.exe
Shellexecute=regsvr.exe
Shell\Open\command=regsvr.exe
Shell=Open
Linebreaks under Windows \r\n are different from Linux's \n.
So try replacing \n with \r\n.
It is probably due to the fact that Windows new lines are \r\n instead of \n.
Also, you should escape the "\", so instead use"\\".