0

I have a script - script.py where i am parsing a json file(the json file has a list of directories to copy to remote host) and the json parsing returns me 2 lists - list1 and list2. I am then passing this list1 and list2 to another script script2 that is called inside script.py

Something like

#!/usr/bin/python
json = 'jsonfile.json'
input_dir='some path1'
output_dir='some path2'
# function to parse json file and get the lists
# call script2.py
path_to_script2/script2.py list1 list2 input_dir output_dir 

I tried

e.g., script2.py list1 list2 input_path output_path This says i cannot pass list type to the script

script2.py has

import os
import sys
import ast

a = ast.literal_eval(sys.argv[1])
b = ast.literal_eval(sys.argv[2])
c = sys.argv[3]
d = sys.argv[4]

print(a)
print(b)

when i try this

script2.py "list1" "list2" input_path output_path - 

I am getting this error-

Traceback (most recent call last):
  File "path/2/script/bexec.p", line 7, in <module>
    a = ast.literal_eval(sys.argv[1])
  File "/usr/lib64/python2.7/ast.py", line 80, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib64/python2.7/ast.py", line 79, in _convert
    raise ValueError('malformed string')
ValueError: malformed string

Here is the json file that i am parsing-

{
"list1": ["./xyz1/abc1/file1.sql",
          "./xyz2/abc2/file2.sql",
          "./xyz3/abc3/file3.sql"
          ],

"list2":[
        {"inp": "./xyz03/abc01/file1.txt",
         "csv": ["./xyz03/abc01/file2.csv"],
          "sql": ["./xyz03/file3.sql"],
          "dat": ["./xyz03/Model/file4.dat"],
       },
       { "inp": "./xyz03/abc01/file2.txt",
         "csv": ["./xyz03/abc01/file2.csv"],
         "sql": ["./xyz03/abc01/file3.sql"],
          "dat": ["./xyz03/Model/file4.dat"]}
        ]
}

This is just a snippet of the json file- It has more values in list2 field.

Or may be i can pass the json.loads(file.json) output to the python script. I hope this provides clarity or i can explain more in case any other details are needed.

1 Answer 1

1

You cannot pass list as command line arguments. Instead you can import the method from another script and call the method with arguments.

script1.py

#!/usr/bin/python
from script2 import script2_main
json = 'jsonfile.json'
input_dir='some path1'
output_dir='some path2'
# function to parse json file and get the lists
# call script2.py
script2_main(list1, list2, input_dir, output_dir)

script2.py

def script2_main(list1, list2, input_dir, output_dir):
    # Do something with the data
    pass

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

2 Comments

Do not put the shebang at the top of script2.py if it is not intended to be run as an executable.
Removed from script2.py

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.