Hey there everyone,

I'm working in windows, writing a python program which runs various programs through command line and pipes the outputs of one into the next program. All the programs accept all instructions in one line then run. I've been using the os.system command to run these. One program though does not work in this manner - it prompts the user for the input file name/location then once it receives this, prompts with the name and desired location of output, etc., until it receives all the information necessary to run. Just providing all the necessary information separated by newlines has not been working and I was hoping that perhaps someone might have suggestions as to how I can make this happen?

Any assistance would be greatly appreciated.

Thank you so much!

Recommended Answers

All 8 Replies

Like:

os.system('program.exe | argument')

Cheers and Happy coding.

Hey there Beat_Slayer,

Essentially I start this particular program by typing its name to command line. The program clears the screen and prompts me for the input file. Once it has received that it requests various other pieces of information, asks me to check a menu of settings and change things, (by pressing "I" to change one setting for example) and so on. Kind of like a continuous question and answer til it has all the information it requires. Here is some of my code as it was yesterday - it might help explain what I am trying to do:

dnamlLocation = "..\\..\\PHYLIP\\dnaml"
dnamlInput = "..\\..\\OutputFiles\\alignedWith0.1SeqError.phy"
dnamlOutput = "..\\..\\OutputFiles\\dnamlOutput.phy"
dnamlOutputTree = "..\\..\\OutputFiles\\dnamlOutputTree.phy"

outputFileResponse = "F" # produce new output file with name and location as specified
changeToSequential = "I"
confirmSettings = "Y"
outputTreeFileResponse = "F" # produce new output file with name and location as specified

cmd = dnamlLocation + ' | ' + dnamlInput + ' | ' + outputFileResponse + ' | ' + dnamlOutput + ' | ' + changeToSequential + ' | ' + confirmSettings + ' | ' + outputTreeFileResponse + ' | ' + dnamlOutputTree

os.system(cmd)

Since then I've been trying a pipeine approach but I don't think that I've been going about it the right way. I think I've got the usage wrong. I tried something like "os.system("myprogram.exe < myinput.txt")" yesterday but I couldn't get it to work.

Any ideas why that might be? Any advice in general would be great.

Thanks so much for replying! :o)

What happens when you run this?

What's the console output?

Sugestions??
Put dnaml on the path and run the script from the OutputFiles folder to avoid all those nasty paths.

Cheers and Happy coding

Console output:

'F' is not recognized as an internal or external command,
operable program or batch file.

Basically it throws an error the second it reaches the outputFileResponse variable (which is equal to 'F').

If possible I would like to avoid putting dnaml in the path because I have a number of programs which work a similar way and this code should be usable by others in a biological setting - adding things to path could be less straight-forward for them.

Hey there woooee,

Thank you so much for your reply. Subprocess did it in the end for me. I hadn't heard of ScriPy though, definitely want to look into that a little more.

One more question if you don't mind, how are you supposed to gracefully close the subprocess once you're done? I tried "subprocess.terminate()" and suddenly my pc got blue screened each time I tried to run my code =P I've never seen anything like it.

Thanks so much for all your help! You guys are lifesavers =)

Popen.terminate()¶

Stop the child. On Posix OSs the method sends SIGTERM to the child. On Windows the Win32 API function TerminateProcess() is called to stop the child.

New in version 2.6.

Popen.kill()¶

Kills the child. On Posix OSs the function sends SIGKILL to the child. On Windows kill() is an alias for terminate().

Source

If you paste the code it will be more easy to help.

Cheers and Happy coding

Thanks once again =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.