26

I've looked all around Google and its archives. There are several good articles, but none seem to help me out. So I thought I'd come here for a more specific answer.

The Objective: I want to run this code on a website to get all the picture files at once. It'll save a lot of pointing and clicking.

I've got Python 2.3.5 on a Windows 7 x64 machine. It's installed in C:\Python23.

How do I get this script to "go", so to speak?

=====================================

Seeing as how this is top result on Google, here's a useful link I found over the years:

http://learnpythonthehardway.org/book/ex1.html

For setup, see exercise 0.

=====================================

As requested, here's the code I'm using:

"""
dumpimages.py
Downloads all the images on the supplied URL, and saves them to the
specified output file ("/test/" by default)

Usage:
    python dumpimages.py http://example.com/ [output]
"""

from BeautifulSoup import BeautifulSoup as bs
import urlparse
from urllib2 import urlopen
from urllib import urlretrieve
import os
import sys

def main(url, out_folder="C:\asdf\"):
    """Downloads all the images at 'url' to /test/"""
    soup = bs(urlopen(url))
    parsed = list(urlparse.urlparse(url))

    for image in soup.findAll("img"):
        print "Image: %(src)s" % image
        filename = image["src"].split("/")[-1]
        parsed[2] = image["src"]
        outpath = os.path.join(out_folder, filename)
        if image["src"].lower().startswith("http"):
            urlretrieve(image["src"], outpath)
        else:
            urlretrieve(urlparse.urlunparse(parsed), outpath)

def _usage():
    print "usage: python dumpimages.py http://example.com [outpath]"

if __name__ == "__main__":
    url = sys.argv[-1]
    out_folder = "/test/"
    if not url.lower().startswith("http"):
        out_folder = sys.argv[-1]
        url = sys.argv[-2]
        if not url.lower().startswith("http"):
            _usage()
            sys.exit(-1)
    main(url, out_folder)
Mr. C
  • 1,652
  • 2
  • 16
  • 22

6 Answers6

22

On windows platform, you have 2 choices:

  1. In a command line terminal, type

    c:\python23\python xxxx.py

  2. Open the python editor IDLE from the menu, and open xxxx.py, then press F5 to run it.

For your posted code, the error is at this line:

def main(url, out_folder="C:\asdf\"):

It should be:

def main(url, out_folder="C:\\asdf\\"):
ciphor
  • 8,018
  • 11
  • 53
  • 70
  • I tried the first one, received the following error message: C:\python23\python: can't open file 'dumpImages.py' Then I realized my mistake: C:\python23\"python dumpImages.py" Then I received a syntax error: EOL while scanning single-quoted string. What the heck does that mean? Thanks! – Mr. C Feb 29 '12 at 03:49
  • It seems your .py file has wrong format. Please post your code. – ciphor Feb 29 '12 at 03:56
  • When I run my helloworld file by option 2 , it works. but when I run another .py file it just shows me the source code.. How to actually make it run ? – Faizan May 14 '13 at 12:38
5

Usually you can double click the .py file in Windows explorer to run it. If this doesn't work, you can create a batch file in the same directory with the following contents:

C:\python23\python YOURSCRIPTNAME.py

Then double click that batch file. Or, you can simply run that line in the command prompt while your working directory is the location of your script.

Mark Ursino
  • 31,209
  • 11
  • 51
  • 83
A.Midlash
  • 143
  • 4
  • 1
    Double-clicking on a `.py` file on Windows will only run it if it's on the System Path. – Edwin Feb 29 '12 at 23:54
4

Since you seem to be on windows you can do this so python <filename.py>. Check that python's bin folder is in your PATH, or you can do c:\python23\bin\python <filename.py>. Python is an interpretive language and so you need the interpretor to run your file, much like you need java runtime to run a jar file.

Gangadhar
  • 1,893
  • 9
  • 9
  • 1
    So, from your example above, this should work? C:\Python23\bin\python\dumpImages.py – Mr. C Feb 29 '12 at 03:22
  • It will be c:\python23\bin\python dumpImages.py. Python.exe is the interpretor and the file name is dumpImages.py – Gangadhar Feb 29 '12 at 03:28
  • Alright. I've got it as specified. It appears to run... however it runs extremely quickly and has no results. I did do this in the cmd prompt: set path=%path%;C:\python23 So now I can access python via cmd.exe. I guess the problem I have now is it isn't asking for parameters. As far as I can tell, the script takes two parameters: a url and a location on my hdd. Whenever I run ith through the python shell: dumpImages(http://www.google.com/, C:\asdf\) I get an invalid syntax error at the semicolon after http. – Mr. C Feb 29 '12 at 03:37
3

use IDLE Editor {You may already have it} it has interactive shell for python and it will show you execution and result.

Junaid
  • 2,084
  • 1
  • 20
  • 30
0

Your command should include the url parameter as stated in the script usage comments. The main function has 2 parameters, url and out (which is set to a default value) C:\python23\python "C:\PathToYourScript\SCRIPT.py" http://yoururl.com "C:\OptionalOutput\"

James
  • 1
-1

If you want to run .py files in Windows, Try installing Git bash Then download python(Required Version) from python.org and install in the main c drive folder

For me, its :

"C:\Python38"

then open Git Bash and go to the respective folder where your .py file is stored :

For me, its :

File Location : "Downloads" File Name : Train.py

So i changed my Current working Directory From "C:/User/(username)/" to "C:/User/(username)/Downloads"

then i will run the below command

" /c/Python38/python Train.py "

and it will run successfully.

But if it give the below error :

from sklearn.model_selection import train_test_split ModuleNotFoundError: No module named 'sklearn'

Then Do not panic :

and use this command :

" /c/Python38/Scripts/pip install sklearn "

and after it has installed sklearn go back and run the previous command :

" /c/Python38/python Train.py "

and it will run successfully.

!!!!HAPPY LEARNING !!!!

  • 1
    Why do we need gitbash here, python program can be execute on command prompt? – Shivam Seth Jun 19 '20 at 10:59
  • Thanks for you efforts! But the second half of the answer has nothing to do with the question – Sergey Shubin Jun 19 '20 at 11:19
  • @ShivamSeth I just gave another method apart from command prompt to run .py files. I use anaconda command prompt to run .py files as well as gitbash. both works fine for me – Pritam Mohapatra Jun 20 '20 at 13:37
  • @SergeyShubin I have posted the 2nd part of my answer to install libraries.Its just for a safe side. If people get any error saying "module not found" they can refer to this method to install libraries through git bash. – Pritam Mohapatra Jun 20 '20 at 13:39