[英]How to run a python script that takes command line arguments with a double click
As a personal project to improve my python skills I created a script that retrieves weather data. 作为提高我的python技能的个人项目,我创建了一个脚本来检索天气数据。 It takes multiple command line arguments to specify the location and what specific information is wanted.
它需要多个命令行参数来指定位置和所需的特定信息。
I'd like to make a second file to run it with specific command line arguments using a double click. 我想制作第二个文件,以双击方式使用特定的命令行参数运行它。 I already learned how to make it into an executable/make a second file execute it.
我已经学习了如何使其成为可执行文件/使第二个文件执行该文件。 However, I don't know how to run it with command line arguments.
但是,我不知道如何使用命令行参数来运行它。
Currently my secondary file (wrapper?.. unsure of terminology) looks like this: 目前,我的辅助文件(包装器?..不确定术语)如下所示:
#! /usr/bin/env python
import weather
weather.main()
This runs but I don't know how to create command line arguments for it without running from the shell. 它可以运行,但是我不知道如何在不从外壳运行的情况下为其创建命令行参数。 I'd like to have a simple executable to run the weather for where I am quickly.
我想有一个简单的可执行文件,可以快速运行天气。
Well, you can call a shell process using the os.system
or the subprocess
module. 嗯,你可以调用使用一个shell进程
os.system
或subprocess
模块。
os.system
takes a string and passes it as a command to a shell. os.system
接受字符串并将其作为命令传递给外壳。
import os
os.system("ls -1")
Whereas subprocess
takes a list of all the arguments (the program itself being the first argument) and passes it as a command. 而
subprocess
获取所有参数的列表(程序本身是第一个参数)并将其作为命令传递。
import subprocess
# Simple command
subprocess.call(['ls', '-1'], shell=True)
Seeing these examples, it's easy to tell that you want the executable program to call either one of these ( os.system
or subprocess
). 看到这些例子,可以很容易地告诉你想要的可执行程序来调用这些(任一个
os.system
或subprocess
)。 I recommend using the latter, as it offers more variety. 我建议使用后者,因为它提供了更多的选择。
If you want more information, I suggest you read the review of subprocess on Python Module of the Week. 如果您想了解更多信息,建议您阅读本周“ Python模块”上的子流程的评论。 .
。
Add to your wrapper script: 添加到包装器脚本中:
import sys
sys.argv[1:] = ['what', 'ever', 'u', 'want']
before the call to weather.main()
. 在调用
weather.main()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.