从bash调用Python脚本与参数 [英] Call Python script from bash with argument
问题描述
python python_script.py
但是,如果我想从我的bash脚本传递一个变量/参数到我的python脚本呢?我该怎么做?
基本上bash会生成一个文件名,然后python会上传它,但是当我打电话给我时,我需要将文件名从bash发送到python
要在bash脚本中执行python脚本,您需要调用与终端中相同的命令。例如
> python python_script.py var1 var2
要访问python中的这些变量,您需要
import sys
打印sys.argv [0]#打印python_script.py
打印sys.argv [1]#prints var1
print sys.argv [2]#prints var2
I know that I can run a python script from my bash script using the following:
python python_script.py
But what about if I wanted to pass a variable / argument to my python script from my bash script. How can I do that?
Basically bash will work out a filename and then python will upload it, but I need to send the filename from bash to python when I call it.
To execute a python script in a bash script you need to call the same command that you would within a terminal. For instance
> python python_script.py var1 var2
To access these variables within python you will need
import sys
print sys.argv[0] # prints python_script.py
print sys.argv[1] # prints var1
print sys.argv[2] # prints var2
这篇关于从bash调用Python脚本与参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!