Solved

Check to see if files exist and download files within Python

Posted on 2007-12-04
3
2,784 Views
Last Modified: 2010-08-05
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!
0
Comment
Question by:dh-s
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
ghostdog74 earned 350 total points
ID: 20408600
>>> 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, download a file from (using the http protocol) online, with the same name that is being checked, into the folder that has been checked.

import os,urllib2
file = "myfile"
folder = "/myfolder"
if not os.path.exists(folder) : os.mkdir(folder,0777)
if not os.path.exists(file):
    ##do download
    page= "http://somewhere"
    result = urllib2.urlopen(page).read() #please check syntax
    open(os.path.join(myfolder,file),"w").write(result)

note: not tested. just to give an idea
0
 

Author Comment

by:dh-s
ID: 20408662
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

0
 

Author Closing Comment

by:dh-s
ID: 31412769
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!
0

Featured Post

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question