Python - Running a script from a script

Questions about programming languages and debugging
Post Reply
User avatar
FaoX
suck-o-fied!
suck-o-fied!
Posts: 76
Joined: 07 Sep 2007, 16:00
16
Contact:

Python - Running a script from a script

Post by FaoX »

Alright so short version of my problem first. I would like to run a python script from within a python script...sort of? With that out there in most cases I would simply import the script as a module and run in from my script. In this case I'm trying to have a python script running that will accept inputs as an intermediary, almost like its own terminal. The basic idea is that while the main script is running I can write up another script, throw it into a sub-directory, and without editing the main script call the new script from it and also be able to pull the output from the new script into the main script for handling. Any ideas?

ps. Pretty sure I brought this idea up a few years back but never finished it and I can't find my original post.
\"The OS is detected as NetBSD (it will even run on your toaster).\"

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: Python - Running a script from a script

Post by Gogeta70 »

I haven't worked in Python for a few months now, so my skills in that are beginning to fade... but it sounds like you essentially want to execute a child process (in this case, a python script) and attach to it's stdin and stdout descriptors. I think os.popen() may be a step in the right direction.
¯\_(ツ)_/¯ It works on my machine...

User avatar
FaoX
suck-o-fied!
suck-o-fied!
Posts: 76
Joined: 07 Sep 2007, 16:00
16
Contact:

Re: Python - Running a script from a script

Post by FaoX »

Good call on popen(). When I was looking around most people made it seem like it was just going to execute the code by itself without being able to pipe in the inputs and outputs but it seems like this might do the trick. The next problem is I need to grab all the file names from a subfolder, that will store the secondary scripts, and then add those files to some form list or array. I want to be able to call the secondary scripts from the main like a command. So to call test1.py I want to just input test1 and be able to dynamically check the subfolder for it.

Currently I'm looking at using something like this to read the folder contents and add the stripped file names into a list.

Code: Select all

>>> import glob
>>> files1 = glob.glob('toolbox/*.py')
>>> print files1
[ 'toolbox\\test1.py', 'toolbox\\test2.py', 'toolbox\\test3.py']
>>> files2 = map(lambda s: s.strip('.py').replace('toolbox\\',''), files1)
>>> print files2
['test1', 'test2', 'test3']
>>>
My question is though is there a way to when this(or something like it) is called it will update a list/matrix/dict and be able to understand that when I input "test1" it be passed to popen() as the actually file address?
\"The OS is detected as NetBSD (it will even run on your toaster).\"

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: Python - Running a script from a script

Post by Gogeta70 »

Well, once again I'm no expert at python, but I think you'll find what you need here:
https://docs.python.org/3/library/filesys.html" onclick="window.open(this.href);return false;

The "pathlib" library looks promising - check the examples under "Basic Use". You may want to consider storing the full path to each python file you want to execute to avoid issues when trying to execute them with os.popen().
¯\_(ツ)_/¯ It works on my machine...

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Python - Running a script from a script

Post by ayu »

If you haven't solved it by tomorrow then I will try to remember to help out, if I don't fall asleep when I get home.
Been having some sleeping issues so pretty tired during the days since the beginning of the year.
Sleeping with some rather heavy drugs atm ^^
"The best place to hide a tree, is in a forest"

User avatar
FaoX
suck-o-fied!
suck-o-fied!
Posts: 76
Joined: 07 Sep 2007, 16:00
16
Contact:

Re: Python - Running a script from a script

Post by FaoX »

Alright so here is where I'm at now. At this point I'm just hashing out parts and getting them to work together so if I'm going about something the wrong way/could be doing it a better way, let me know.

Code: Select all

import os
import glob
import platform

#Detect if being run on windows or *nix
if platform.system() == 'Windows':
        tooldir = '\\toolbox\\'
elif platform.system() == 'Linux':
        tooldir = '/toolbox/'
else:
        tooldir = '/toolbox/'
        print "OS Detection Failed Defaulting to *nix"

#main input loop
command = ''
toolpath = os.getcwd()+tooldir
while command != 'quit':
        #Find and Clean all file names in toolbox
        files_LG = glob.glob('toolbox/*.py')
        files_LC = map(lambda s: s.strip('.py').replace(tooldir , ''), files_LG)
        #Read and parse input
        command = raw_input('>:')
        if command == 'help':
                print 'The following scripts are ready to be executed: '
                print files_LC
        elif command != '':
                print toolpath+command+'.py'
        else:
                print 'failed'
My current problem is I want(for the moment) that when i give the input of "help" it will list all the files in the sub 'toolbox' without the path or extension. I can't seem to get rid of the sub from the list. Windows will output "toolbox\\test1" and *nix will output "toolbox/test1". What I plan on doing eventually is when you call the help command it will list the file names in toolbox and then I want it to read a (to be determined) line in each file and put them together so "help" will output "test1 - does this with these arguments".
\"The OS is detected as NetBSD (it will even run on your toaster).\"

pseudo_opcode
cyber messiah
cyber messiah
Posts: 1201
Joined: 30 Apr 2006, 16:00
17
Location: 127.0.0.1

Re: Python - Running a script from a script

Post by pseudo_opcode »

FaoX wrote:Alright so short version of my problem first. I would like to run a python script from within a python script...sort of? With that out there in most cases I would simply import the script as a module and run in from my script. In this case I'm trying to have a python script running that will accept inputs as an intermediary, almost like its own terminal. The basic idea is that while the main script is running I can write up another script, throw it into a sub-directory, and without editing the main script call the new script from it and also be able to pull the output from the new script into the main script for handling. Any ideas?

ps. Pretty sure I brought this idea up a few years back but never finished it and I can't find my original post.
I am not sure why would you want to do that, but it does seem like you're upto something interesting.

Unless choice of language(Python) and time is a constraint for you, you should look into LISP. With LISP's ability to treat data as code and vice versa is something which will make what you're trying to do, trivial. By trivial, i mean one line of code. Not to mention, lisp has a feature called "macros", something tells me you'd love that too.

Beware though, going deeper into LISP will open a whole new pandora's box for you, if you go down the rabbit hole, you will develop a new appreciation for computer science and mathematics.

If you'd like, you should read one of my favourite articles by Paul Graham who's co-founder of ycombinator. Its called "Revenge of the nerds"

http://www.paulgraham.com/icad.html

Post Reply