Link to home
Start Free TrialLog in
Avatar of dh-s
dh-s

asked on

Check to see if files exist and download files within Python

Hello,

I wish to be able to check (in a python script) to see if a file and/or folder already exists on a users hard drive, and if it doesn't exist, download a file from (using the http protocol) online, with the same name that is being checked, into the folder that has been checked.

Also, if the folder that I wish to download into doesn't exist, I would like to create it.

Currently, I am using a very hackish method, as I am on Linux, I am effectively running commands using "os.popen" to create the directory I wish using mkdir (though it cannot check to see if it already exists), and wget to download it into the file.

I wish to make this program/script as cross platform as possible, which means I need to avoid using these, meaning I need to be able to do it within Python.

If there is a way to do it within Python, but only for a specific platform, I am working on Linux, so I need it to be able to at least work on that.

Thank you for your help!
ASKER CERTIFIED SOLUTION
Avatar of ghostdog74
ghostdog74

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dh-s
dh-s

ASKER

HI,

"os.path.exists(folder)" to check if the folder/file exists works fine! Thanks.

Unfortuantly, urllib2 does not: [see code snippet]

Do you know where I can get the urllib2 module? As I looked in the Python documentation, and it does exist, but I can't work out where I can get it from to include it with my program/script, or get the user to install it.

Thanks!

>>> import os.urllib2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named urllib2

Open in new window

Avatar of dh-s

ASKER

I got the problem I had with the comment below fixed:

I mis-read what you had written, and didn't see that it was a comma, not a full stop (in import os,urllib2). It works when that is fixed, thanks!