[英]passing argument to python from shell script
I have run through some similar questions but none seem to answer my doubt, I am trying to pass the argument to python script from my bash script. 我遇到了一些类似的问题,但是似乎都没有回答我的疑问,我正在尝试将参数从bash脚本传递给python脚本。 I don't see errors nor I get the required output. 我没有看到错误,也没有得到所需的输出。 What am I doing wrong here? 我在这里做错了什么?
python: 蟒蛇:
import os
import glob
import unicodecsv as csv
import pandas as pd
import codecs
import sys
OUTPUT_PATH=sys.argv[2]
def createFolder(directory):
# print('createFolder')
try:
if not os.path.exists(directory):
# print('createFolder')
os.makedirs(directory)
# print(directory)
except OSError:
print ('Error: Creating directory. ' + directory)
createFolder(OUTPUT_PATH + 'csv20_out/')
bash script: bash脚本:
INPUT_PATH='home/pg/Public/test_data/test0002_2files_to_1flow/csv64_in/'
OUTPUT_PATH='home/pg/Public/test_data/test0002_2files_to_1flow/'
cd /
cd home/pg/Documents/LMS/kanjiforbeginner/convertToYAML/
cd converts_csv642csv20
python csvFile.py $INPUT_PATH $OUTPUT_PATH
Should create a folder csv20_out 应该创建一个文件夹csv20_out
when I print OUTPUT_PATH, I get 'home/pg/Public/test_data/test0002_2files_to_1flow/' 当我打印OUTPUT_PATH时,得到“ home / pg / Public / test_data / test0002_2files_to_1flow /”
The contents of sys.argv look like this: sys.argv的内容如下所示:
['csvFile.py', 'inputPath', 'outputPath']
To get the outputPath you need to access sys.argv[2] in your case. 要获得outputPath,您需要访问sys.argv [2]。
By convention, the first argument is the name of the program itself—that is, the first thing specified on the command line ( whatever_name.py
). 按照惯例,第一个参数是程序本身的名称,即在命令行上指定的第一件事( whatever_name.py
)。
So argv[0]
is the program's name, argv[1]
is your input path, and argv[2]
is your output path. 因此argv[0]
是程序的名称, argv[1]
是您的输入路径,而argv[2]
是您的输出路径。
So, as the other commenters have pointed out, you need to use argv[2]
instead of argv[1]
This is because in bash argv[0]
is the python script being executed and argv[1]
in your case becomes '$INPUT_PATH'
因此,正如其他评论者所指出的那样,您需要使用argv[2]
而不是argv[1]
这是因为在bash中argv[0]
是正在执行的python脚本,而argv[1]
在您的情况下变为'$INPUT_PATH'
But, that won't solve your problem. 但是,那不会解决您的问题。 In your script, you need to remove the '
surrounding $INPUT_PATH
and $OUTPUT_PATH
so that the last line becomes, 在脚本中,您需要删除'
周围的$INPUT_PATH
和$OUTPUT_PATH
以便最后一行成为
python csvFile.py $INPUT_PATH $OUTPUT_PATH
'$INPUT_PATH'
sends the string
$INPUT_PATH
to your python script, whereas, '$INPUT_PATH'
将string
$INPUT_PATH
发送到您的python脚本,而
$INPUT_PATH
sends 'home/pg/Public/test_data/test0002_2files_to_1flow/csv64_in/'
$INPUT_PATH
发送'home/pg/Public/test_data/test0002_2files_to_1flow/csv64_in/'
or whatever value you choose to assign to $INPUT_PATH
或您选择分配给$INPUT_PATH
任何值
And lastly, you need to change, the values of $INPUT_PATH
and $OUTPUT_PATH
最后,您需要更改$INPUT_PATH
和$OUTPUT_PATH
的值
INPUT_PATH='home/pg/Public/test_data/test0002_2files_to_1flow/csv64_in/'
OUTPUT_PATH='home/pg/Public/test_data/test0002_2files_to_1flow/'
INPUT_PATH='home/pg/Public/test_data/test0002_2files_to_1flow/csv64_in/'
OUTPUT_PATH='home/pg/Public/test_data/test0002_2files_to_1flow/'
needs to become 需要成为
INPUT_PATH='/home/pg/Public/test_data/test0002_2files_to_1flow/csv64_in/'
OUTPUT_PATH='/home/pg/Public/test_data/test0002_2files_to_1flow/'
INPUT_PATH='/home/pg/Public/test_data/test0002_2files_to_1flow/csv64_in/'
OUTPUT_PATH='/home/pg/Public/test_data/test0002_2files_to_1flow/'
The /
at the beginning gives the full path to the directory or file, if you didn't use the /
then it becomes a relative path. 开头的/
会提供目录或文件的完整路径,如果您不使用/
那么它将成为相对路径。
In your bash script, you're navigating folders using cd
as a result, the folder 在bash脚本中,您正在使用cd
导航文件夹,即文件夹
'home/pg/Public/test_data/test0002_2files_to_1flow/csv_out'
will get created under the directory, 将在目录下创建,
home/pg/Documents/LMS/kanjiforbeginner/convertToYAML/
Adding the /
at the beginning to $INPUT_PATH
and $OUTPUT_PATH
as shown before would ensure that your code: 如前所示,在$INPUT_PATH
和$OUTPUT_PATH
的开头添加/
可以确保您的代码:
edit: 编辑:
As Kim has suggested below, the last line in the script should be: 就像Kim在下面建议的那样,脚本的最后一行应该是:
python csvFile.py "$INPUT_PATH" "$OUTPUT_PATH"
This is, in case $INPUT_PATH
or $OUTPUT_PATH
has spaces or special characters. 这是在$INPUT_PATH
或$OUTPUT_PATH
带有空格或特殊字符的情况下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.