从bash脚本将变量传递给python函数
我真的很努力地试图将变量从bash脚本传递给我制作的python函数.我查看了有关同一问题的大量帖子,似乎无法理解我所缺少的内容.
I am really struggling trying to figure out to pass variables from a bash script to a python function I have made. I have looked over numberous posts about the same issue and cant seem to understand what I am missing.
我有一个名为fname_function.py的python函数脚本:
I have a python function script named fname_function.py:
from glob import glob
import os
import sys
first_arg=sys.argv[1]
second_arg=sys.argv[2]
third_arg=sys.argv[3]
def gFpath(reach,drive,date):
list1 = glob(os.path.normpath(os.path.join(drive, reach, date,'*')))
list2 =[]
for afolder in list1:
list2.append(glob(os.path.normpath(os.path.join(drive, reach, date, afolder, 'x_y_class?.asc'))))
return list2
if __name__=='__main__':
gFpath(first_arg,second_arg,third_arg)
我的bash脚本如下:
And my bash script looks like:
reach="R4a"
drive= "D:\\"
dte="2015_04"
fnames=$(python fname_function.py "$reach" "$drive" "$dte")
for fname in $fnames; do echo "Script returned $fname"; done
变量正在传递给python脚本,但是我似乎无法将 list2
返回到我的shell脚本.
The variables are being passed to the python script, but I cant seem to get list2
back to my shell script.
谢谢
Dubbbdan
您可以直接运行Python文件,例如 python fname_function.py"$ reach""$ drive""$ dte"
You can just run the Python file directly, like python fname_function.py "$reach" "$drive" "$dte"
但是,在这种情况下, sys.argv [0]
将是 fname_function.py
,因此您需要将 first_arg
设置为 sys.argv [1]
并同时增加其他数字.
However, sys.argv[0]
will be fname_function.py
in this case, so you'll want to set first_arg
to sys.argv[1]
and increment the other numbers as well.
此外,您不会在Python脚本中输出任何内容.您应该将脚本的结尾读为:
Also, you don't output anything in your Python script. You should make the end of your script read:
if __name__=='__main__':
fnames = gFpath(first_arg,second_arg,third_arg)
for fname in fnames:
print(fname)
,将在每行上从 gFpath
中打印出1个结果.
which will print out 1 result from gFpath
on each line.