Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
    
6  
What similar questions did you read, what key points did they not cover, and what parts did you not understand? – Kevin Aug 31 '15 at 19:54
    
Run programs using the subprocess module; parse the arguments using the argparse module. – dsh Aug 31 '15 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. – dnzzcn Aug 31 '15 at 20:02
    
Have the currently provided answers helped answer your question? – Juxhin Aug 31 '15 at 20:04
up vote 1 down vote accepted

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.

share|improve this answer
    
Thanks a lot Juxhin. – dnzzcn Aug 31 '15 at 20:09
    
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. – Juxhin Aug 31 '15 at 20:11
    
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 Aug 31 '15 at 20:20
    
@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. – Juxhin Aug 31 '15 at 20:48

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

share|improve this answer
    
I will try this for my system. Thank you hd1. – dnzzcn Aug 31 '15 at 20:10
1  
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 – Filipe Aug 31 '15 at 20:11
    
Of course, but fork(2) is an expensive call that I avoid making. – hd1 Aug 31 '15 at 20:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.