2

There are some similar questions that are asked; but, either the answers didn't cover a specific part that is a key point for me or I just couldn't understand. So, let me explain my question:

I want to create 3 python scripts. Let's call them p1.py, p2.py and p3.py . When I run p1, it will gather the time information with

import datetime    
captureTime = datetime.datetime.now()

then it will change the format of the date and store it with a variable as shown below

folderName = captureTime.strftime('%Y-%m-%d-%H-%M-%S')

Then, it will create a folder named by the value of the string 'folderName'.

After this part, I don't know what to do.

p1 should run p2 and p3, and pass the value of 'folderName' to them, then stop; and, p2 and p3 should create files under the folder named by the value of 'folderName'.

Thank you.

6
  • docs.python.org/3/library/sys.html#sys.argv Commented Aug 31, 2015 at 19:54
  • 6
    What similar questions did you read, what key points did they not cover, and what parts did you not understand? Commented Aug 31, 2015 at 19:54
  • Run programs using the subprocess module; parse the arguments using the argparse module. Commented Aug 31, 2015 at 19:59
  • stackoverflow.com/questions/16048237/… In this one for example, Abhranil Das's answer explains how to do it in a single script. You call another script inside a script and use the variables of it or vice versa. However, I want to send the variables to another script that works independently. Commented Aug 31, 2015 at 20:02
  • Have the currently provided answers helped answer your question? Commented Aug 31, 2015 at 20:04

2 Answers 2

4

In p1:

import p2
args = ['foo', bar]
p2.main(args)

In p2:

def main(args): 
    do_whatever()

if __name__ == '__main__':
  main()

p3 will be of a similar structure to p2

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

3 Comments

I will try this for my system. Thank you hd1.
This way everything is going to run on the same process space and not parallel unless he uses threads. If the first process is to be a supervisor, it's best to fork using subprocess.Popen like suggested by @Juxhin
Of course, but fork(2) is an expensive call that I avoid making.
1

You can make use of the subprocess module do perform external commands. It is best to start with much simpler commands first to get the gist of it all. Below is a dummy example:

import subprocess
from subprocess import PIPE

def main():
    process = subprocess.Popen('echo %USERNAME%', stdout=PIPE, shell=True)
    username = process.communicate()[0]
    print username #prints the username of the account you're logged in as

    process = subprocess.call('python py1.py --help', shell=True)
    process = subprocess.call('python py2.py --help', shell=True)
    process = subprocess.call('python py3.py --help', shell=True)

if __name__ == '__main__':
    main()

This will grab the output from echo %USERNAME% and store it. It will also run your three scripts but do nothing fancy with them. You can PIPE the output of the scripts as shown in the first example and feed them back in to your next script.

This is NOT the only way to do this (you can import your other scripts). This is nice if you would like to have an external master script to control and manipulate all your child scripts.

If you haven't checked our argparse yet, you should.

3 Comments

You are welcome. If you would like me to elaborate more on the answer (possibly show how you can chain on script to another) let me know.
I will try to implement this to my problem. If I need further information, I'm definitely going to consult to you. Thanks again.
@dnzzcn - please mark any of the answers as correct if they have been able to answer your question so that we may close this post off. If not explain what you are still having issues with so that we may help.

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.