简体   繁体   English

如何双击运行带有命令行参数的python脚本

[英]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.systemsubprocess模块。

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.systemsubprocess )。 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.

相关问题 如何使用从命令行获取 arguments 的 function 运行 py 脚本? - How to run py script with function that takes arguments from command line? 如何修改 python 脚本,使其从命令行获取 arguments - How to modify a python script so it takes arguments from the command line 在shell脚本中运行python命令行参数 - run python command line arguments in shell script 在python脚本中运行命令行参数 - Run command line arguments in python script 如何打包一个接受命令行参数并且还具有依赖关系的 Python 脚本? - How do I package a single python script which takes command line arguments and also has dependencies? 需要从另一个Python脚本调用需要命令行参数的python脚本 - python script that takes command line arguments needs to be called from another python script 使用命令行参数运行Python程序的Shell脚本 - Shell script to run Python program with command line arguments 如何使用python中的click制作命令行程序,该命令采用运行不同脚本的单独命令参数? - How do I make a command line program using click in python that takes separates command parameters that run different scripts? 如何在 Windows 命令行中使用参数运行 Python 脚本 - How do I run Python script using arguments in windows command line 如何在 Chrome OS 上使用命令行 arguments 运行 Python 脚本? - How do I run a Python script WITH command-line arguments on Chrome OS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM