Reputation: 15
I need to create a skip function in python which skips my download code if the file already exists.
How the function should work: (If the file exists then no need to run this code, just skip to next code. And if it doesn't exist then run this code and then run the next code)
Filecheck = os.path.join(OUTPUT_FOLDER,"test"+version+"exe")
print Filecheck
if not os.path.exists(Filecheck):
base_url = urlJoin(LINK, + version + "_multi.exe")
print base_url
filename2 = "%s_%s_.exe" % (software.capitalize(),version)
original_filename = os.path.join(OUTPUT_FOLDER, filename2)
if writeFile(original_filename, httpRequestFile(base_url), "wb") and os.path.exists(original_filename):
print "Download done"
Upvotes: 1
Views: 10153
Reputation: 123662
if not os.path.exists(<path-to-file>):
download_file()
I'm guessing this is what you mean, though it's very hard to tell.
filename = "%s_%s_.exe" % (software.capitalize(),version)
if not os.path.exists(os.path.join(OUTPUT_FOLDER, filename)):
base_url = urlJoin(LINK, + version + "_multi.exe")
writeFile(original_filename, httpRequestFile(base_url), "wb")
FYI if you use requests
you don't need httpRequestFile
, so you can simplify your code to:
import requests
from urllib2 import urljoin
filename = "%s_%s_.exe" % (software.capitalize(),version)
if not os.path.exists(os.path.join(OUTPUT_FOLDER, filename)):
with open(filename, "wb") as fp:
fp.write(requests.get(urljoin(LINK, version + "_multi.exe")).content)
Upvotes: 5