3

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

0

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.