简体   繁体   中英

passing arguments to python script from command line

I am really new to python and pandas, I am trying to execute a python script using arguments in the command line but I got an error, here is my script

 #!/usr/bin/python
 import sys, pandas as pd

 df1 = pd.read_table(sys.argv[0], sep="\t", header=0)
 df2 = pd.read_table(sys.argv[1], sep="\t", header=0)

 df_merge = pd.merge(left=df1, right=df2, left_on=sys.arg[2],  right_on=sys.arg[3])
 df_merge.to_csv(sys.arg[4], sep="\t")

And I got the following error: KeyError: u'no item named file.out' , any help would be apreciated

My command line statement is: merge_files.py file1.out file2.out col1 col3 test

sys.argv[0] is the name of the script, ie merge_files.py . You can see this by inserting print(sys.argv) at the beginning of your script. Try increasing all your indices by 1.

The first argument sys.argv[0] is the name of the script.

sys.argv : The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not).

Please look here for more details.

#!/usr/bin/python
import sys, pandas as pd

df1 = pd.read_table(sys.argv[1], sep="\t", header=0)
df2 = pd.read_table(sys.argv[2], sep="\t", header=0)

df_merge = pd.merge(left=df1, right=df2, left_on=sys.arg[3],     right_on=sys.arg[4])
df_merge.to_csv(sys.arg[5], sep="\t")

This should work.

increase all your indexes by 1, because sys.argv[0] - is a name of the python script.

Ie

df1 = pd.read_table(sys.argv[1], sep="\t", header=0)
df2 = pd.read_table(sys.argv[2], sep="\t", header=0)

and so on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM