I want to download xml file from issue tracking system one by one.It produces error message when file not exist in the repository. I include the python script to better understand my problem.
My code:
import urllib.request
for i in range(0,1000):
issue_id1='DERBY-'+str(i)
url ="https://issues.apache.org/jira/si/jira.issueviews:issue-xml/"+issue_id1+'/'+issue_id1+'.xml'
s=urllib.request.urlopen(url)
contents = s.read()
file = open(issue_id1+'.xml', 'wb')
file.write(contents)
file.close()
Stack Track:
Traceback (most recent call last):
File "/PhP/Learning/xmldownlaod.py", line 10, in <module>
s=urllib.request.urlopen(url)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 469, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 579, in http_response
'http', request, response, code, msg, hdrs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 507, in error
return self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 441, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 587, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
Python Check if Directory Existsisdir() method checks if a directory exists. It returns False if you specify a path to a file or a directory that does not exist. If a directory exists, isdir() returns True. The isdir() method takes in one argument: the directory whose existence you want to verify.
isfile() Method to check if file exists. os. path. isfile() method in Python is used to check whether the specified path is an existing regular file or not.
Method 2: Find the path to the given file using os. In order to obtain the Current Working Directory in Python, use the os. getcwd() method. This function of the Python OS module returns the string containing the absolute path to the current working directory.
Python has a built-in open() function to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly. We can specify the mode while opening a file.
Python uses "try except" blocks for error handling
import urllib.request
from urllib.error import URLError # the docs say this is the base error you need to catch
for i in range(0,1000):
issue_id1='DERBY-'+str(i)
url ="https://issues.apache.org/jira/si/jira.issueviews:issue-xml/"+issue_id1+'/'+issue_id1+'.xml'
try:
s=urllib.request.urlopen(url)
contents = s.read()
except URLError:
print('an error occurred while fetching: "{}"'.format(url))
continue # skip this url and proceed to the next
file = open(issue_id1+'.xml', 'wb')
file.write(contents)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With