0

I am trying to run a Python script a.py from another Python script scheduler.py and I want to pass a list as argument something like:

Scheduler.py:

t = {"code": 161123134, "name": "task2", "domain": "www.google.com", "type": "Type1", "keywords": ["bai2", "yin4", "jiao3", "yi8", "ping1", "tai3"]}

hourTasks = json.dumps(t)
os.system("python a.py " + hourTasks)

a.py

task = sys.argv[1:]
task = json.loads(task)

However It gives me an error the JSON object must be str, bytes or bytearray, not 'list'. Anyone know what the problem is?

3
  • It might be easier to do a bit of rewriting to a.py, and use it as a module instead. Otherwise, consider using the subprocess module instead of os.system(). Commented Mar 21, 2018 at 3:02
  • 1
    The error is unrelated to running a Python script from another script. task in json.loads(task) is a list, not a string. Commented Mar 21, 2018 at 3:03
  • 1
    You want sys.argv[1] (the first argument), not sys.argv[1:] (a list containing the first, and any other, arguments). Commented Mar 21, 2018 at 3:28

3 Answers 3

1

Try this:

Scheduler.py:

import json
import subprocess

t = {"code": 161123134, "name": "task2", "domain": "www.google.com", "type": "Type1", "keywords": ["bai2", "yin4", "jiao3", "yi8", "ping1", "tai3"]}

task = json.dumps(t)
subprocess.call(["python", "a.py", task])

a.py:

import json
import sys                                                                                        

task = sys.argv[1]
t = json.loads(task)
Sign up to request clarification or add additional context in comments.

Comments

0

from one script you should import the other and there is no need to use json between your two scripts, just pass an python dict instead.

Comments

0

You could use the method of the dicts, to read the keys and the values. And before that do a counter and membership check like. For x in xs:

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.