2

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

user2380782
  • 1,542
  • 4
  • 22
  • 60

3 Answers3

3

sys.argv[0] is the name of the script, i.e. 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.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89
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.

gaganso
  • 2,914
  • 2
  • 26
  • 43
0

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

I.e.

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

MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419