Python——如何运行.py文件?

时间:2021-03-29 23:56:15

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.

我在Windows 7 x64机上安装了Python 2.3.5。它是安装在C:\ Python23。

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

我怎样才能让这个脚本“go”呢?

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

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

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

哇。35 k的观点。这是我多年来发现的一个有用的链接。

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

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

For setup, see exercise 0.

对于设置,请参见练习0。

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

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

FYI: I've got zero experience with Python. Any advice would be appreciated.

我对Python没有任何经验。如有任何建议,我们将不胜感激。

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)

5 个解决方案

#1


15  

On windows platform, you have 2 choices:

在windows平台上,你有两个选择:

  1. In a command line terminal, type

    在命令行终端中,输入

    c:\python23\python xxxx.py

    c:\ python23 \ python xxxx.py

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

    从菜单中打开空闲的python编辑器,并打开xxxx。py,然后按F5运行它。

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\\"):

#2


3  

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.

由于您似乎在windows上,您可以这样做,所以python 。检查python的bin文件夹是否在您的路径中,或者您可以执行c:\python23\bin\python 。Python是一种解释性语言,所以您需要解释器来运行文件,就像您需要java运行时来运行jar文件一样。

#3


3  

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

使用空闲编辑器{您可能已经有它}它有python的交互shell,它将显示您的执行和结果。

#4


2  

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:

通常您可以双击Windows资源管理器中的.py文件来运行它。如果这不起作用,您可以在相同的目录中创建一个具有以下内容的批处理文件:

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.

然后双击该批处理文件。或者,您可以在命令提示符中运行这一行,而您的工作目录是脚本的位置。

#5


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\"

您的命令应该包括url参数,如脚本使用注释中所述。主函数有两个参数,url和out(设置为默认值)C:\python23\python“C:\PathToYourScript脚本”。py " http://yoururl.com " C:\ OptionalOutput \”

#1


15  

On windows platform, you have 2 choices:

在windows平台上,你有两个选择:

  1. In a command line terminal, type

    在命令行终端中,输入

    c:\python23\python xxxx.py

    c:\ python23 \ python xxxx.py

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

    从菜单中打开空闲的python编辑器,并打开xxxx。py,然后按F5运行它。

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\\"):

#2


3  

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.

由于您似乎在windows上,您可以这样做,所以python 。检查python的bin文件夹是否在您的路径中,或者您可以执行c:\python23\bin\python 。Python是一种解释性语言,所以您需要解释器来运行文件,就像您需要java运行时来运行jar文件一样。

#3


3  

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

使用空闲编辑器{您可能已经有它}它有python的交互shell,它将显示您的执行和结果。

#4


2  

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:

通常您可以双击Windows资源管理器中的.py文件来运行它。如果这不起作用,您可以在相同的目录中创建一个具有以下内容的批处理文件:

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.

然后双击该批处理文件。或者,您可以在命令提示符中运行这一行,而您的工作目录是脚本的位置。

#5


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\"

您的命令应该包括url参数,如脚本使用注释中所述。主函数有两个参数,url和out(设置为默认值)C:\python23\python“C:\PathToYourScript脚本”。py " http://yoururl.com " C:\ OptionalOutput \”