I am downloading multiple CSV files from a website using Python. I would like to be able to check the response code on each request.
I know how to download the file using wget, but not how to check the response code:
os.system('wget http://example.com/test.csv')
I've seen a lot of people suggesting using requests, but I'm not sure that's quite right for my use case of saving CSV files.
r = request.get('http://example.com/test.csv')
r.status_code # 200
# Pipe response into a CSV file... hm, seems messy?
What's the neatest way to do this?
tempfilemodule's NamedTemporaryFile() static method) then then rename it only if the transfer is successful. If there's an older version of the file present I'd use a "link dance" to hard link it to a ".old" or ".$(date ...)" name, then hard link the old name to the temporary file (then unlinking the temp. file leaving only the good file). Using this process will provide the best data integrity guarantees.