简体   繁体   English

如何自动运行脚本

[英]How to auto-run a script

I created a script that will tell me what to wear in the morning based on the weather (ie rain slicker if it will rain, heavy jacket if it will be cold, etc). 我创建了一个脚本,告诉我早上应该根据天气穿什么(如果会下雨就会下雨,如果下雨就会出现厚重的夹克等)。 I have fairly basic programming experience with python and the script works perfectly, but I want to be able to create a file that I can just double-click from my desktop and the script will automatically run. 我有相当基本的编程经验与python和脚本完美的工作,但我希望能够创建一个文件,我只需从我的桌面双击,脚本将自动运行。

My goal is to be able to simply double click [something] in the morning and it will automatically run the script and thus tell me what to wear. 我的目标是能够简单地在早上双击[某事物],它将自动运行脚本,从而告诉我穿什么。 How could I go about doing this? 我怎么能这样做?

System Specifications: 系统规格:

  • python 蟒蛇
  • Mac OSX Mac OSX

This worked for me on Snow Leopard: 这适用于Snow Leopard:

-Put the python script on the desktop. - 在桌面上放置python脚本。

-Right click on the script file, and choose "Get info" - 右键单击​​脚本文件,然后选择“获取信息”

-Find "Open With", and choose "Python Launcher" from the dropdown box - 找到“打开方式”,然后从下拉框中选择“Python启动器”

Now double-clicking the script file will run the script in a new terminal window. 现在双击脚本文件将在新的终端窗口中运行脚本。

I'm not sure what versions of OS X come with the Python Launcher application. 我不确定Python Launcher应用程序附带什么版本的OS X. If you don't have that, you can solve it with a couple extra steps: 如果你没有,你可以通过几个额外的步骤来解决它:

-Put the python script anywhere - 将python脚本放在任何地方

-Create a shell script on the desktop with one line: - 使用一行在桌面上创建一个shell脚本:

python "/Users/john/scripts/what-to-wear.py"

(Where I've assumed your script is called what-to-wear.py and is in /Users/john/scripts. Be aware that you do need to use an absolute path.) (我假设您的脚本名为what-to-wear.py,位于/ Users / john / scripts中。请注意,您确实需要使用绝对路径。)

-Make the shell script executable. - 使shell脚本可执行。 In a terminal: 在终端:

chmod 755 what-to-wear-shell-script

-Double clicking the shell script should run it in a terminal, running your python script. - 双击shell脚本应该在终端运行它,运行你的python脚本。

What you want to do is create an executable file. 你想要做的是创建一个可执行文件。

I've never used a Mac or Python, but look at this question and the first answer: 我从未使用过Mac或Python,但看看这个问题和第一个答案:

How can I create a directly-executable cross-platform GUI app using Python? 如何使用Python创建可直接执行的跨平台GUI应用程序?

Seems http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html is what you're looking for 似乎http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html正是您要找的

Use a batch file to make it automatic 使用批处理文件使其自动化

Example : 1. Open Notepad -> type the following. 示例:1。打开记事本 - >键入以下内容。 This one's for Windows..It might give you a hint :start C:\\Python34\\python.exe(your python file location)Your *.py file location. 这个用于Windows ..它可能会给你一个提示:启动C:\\ Python34 \\ python.exe(你的python文件位置)你的* .py文件位置。

:end Save this with a *.bat extension That's it ..you can configure more on this batch,I guess batch is the automation for day to day script :结束使用* .bat扩展名保存它就是它..你可以在这个批处理上配置更多,我想批处理是日常脚本的自动化

In Linux/unix based OS , add #!/usr/bin/python3 line on top of your script file with extension .py , if you have python version 3. Or change it to the version installed in the machine Further , make the file executable by 在基于Linux / unix的操作系统中,如果你有python版本3,在扩展名为.py脚本文件的顶部添加#!/usr/bin/python3行。或者将其更改为机器中安装的版本。进一步,制作文件可执行的

sudo chmod +x <fileName>

for windows, add windows python path and make the file executable 对于Windows,添加Windows python路径并使文件可执行

You want the script to download the weather information online and output the clothes based on your predefined rules? 您希望脚本在线下载天气信息并根据预定义的规则输出衣服吗?

If this is the case, use urllib to download the page and do some ad hoc parsing over the downloaded html page to get the whether information. 如果是这种情况,请使用urllib下载页面并对下载的html页面进行一些临时解析以获取信息。 And write your logic using nested IF THEN ELSE blocks. 并使用嵌套的IF THEN ELSE块编写逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM