I want to execute a command in terminal from a Python script.
./driver.exe bondville.dat
This command is getting printed in the terminal, but it is failing to execute.
Here are my steps:
echo = "echo"
command="./driver.exe"+" "+"bondville.dat"
os.system(echo + " " + command)
It should execute the command, but it's just printing it on terminal. When feeding the same thing manually it's executing. How do I do this from a script?
The echo terminal command echoes its arguments, so printing the command to the terminal is the expected result.
Are you typing echo driver.exe bondville.dat and is it running your driver.exe program?
If not, then you need to get rid of the echo in the last line of your code:
os.system(command)
You can use the subprocess.check_call module to run the command, you don't need to echo to run the command:
from subprocess import check_call
check_call(["./driver.exe", "bondville.dat"])
Which is equivalent to running ./driver.exe bondville.dat from bash.
If you wanted to get the output you would use check_outout:
from subprocess import check_output
out = check_output(["./driver.exe", "bondville.dat"])
In your own code you are basically echoing the string command not actually running the command i.e echo "./driver.exe bondville.dat" which would output ./driver.exe bondville.dat in your shell.
Try this:
import subprocess
subprocess.call("./driver.exe bondville.dat")
Related
I am trying to run a script remotely on a server and I intend to use something along the following lines:
nohup ./script.py > runtime.out 2> runtime.err & and monitor the script's progress with tail -f runtiime.out. The problem I am having is that the redirect doesn't seem to work as expected. For the purposes of my problem my problem can be reproduced as described below:
script.py:
#!/usr/bin/env python3
import time
if __name__=='__main__':
for i in range(1000):
print("hi")
time.sleep(1)
Then in shell run ./print.py > a.out &. This will give the PID of the proccess and will exit as expected. However a.out is empty despite the program running. Also if i do ./print.py > a.out without the '&' the a.out file remains empty until I Ctrl-C the command. Then it displays all expected output until the termination of the script.
I thought the ">" redirected continuously the stdout and stderr and not only at command completion.
The simplest way to do that is just by using -u flag of the python command. It should look like that:
nohup python3 -u script.py > runtime.out 2> runtime.err &
According to the python3 --help:
-u : force the stdout and stderr streams to be unbuffered;
this option has no effect on stdin; also PYTHONUNBUFFERED=x
Using print("hi", flush=True) will keep forcing the stream to flush contents, so it will continuously update the output file. I don't have enough information about your program to suggest alternatives, but I would look for a better method if possible.
I have a Python script that I want to be able to debug from the command line. Let's say it's called simple.py, and it contains the following code:
var1 = "Hello"
var2 = "World"
print(var1, var2)
I want to debug this. Of course I can use the debugger from the command line:
python -m pdb simple.py
But I want to debug this specifically from within another python file. I've tried using subprocess to do this, by putting this code into another script, debug.py:
import subprocess
subprocess.Popen(["bash", "-ic", "python -m pdb simple.py"])
When I run it in a Bash shell, this happens:
$ python debug.py
$ > /[path]/simple.py(1)<module>()
-> var1 = "Hello"
(Pdb) n
bash: n: command not found
$
So I've typed "n", expecting the debugger to move to the next line. Instead, it seems to just go straight back to Bash, and the debugger doesn't do anything.
Any idea why this happens? Is there a way to spawn a Bash shell containing a Python debugger from within a Python script, which I can actually use to debug?
Your debug.py finish its run and you are back to the shell (try typing ls instead, and see what happens)
What you are looking for is a way to interact with the other process, you need to get the input from your stdin and pass it to the other process stdin. It can looks something like:
import subprocess
p = subprocess.Popen(["python3", "-m", "pdb", "test.py"])
while True:
cmd = input()
p.stdin.write(cmd.encode())
I am trying to run a specific command from Python to Powershell:
The command works as expected in Powershell. The command in Powershell is as following:
gpt .\Method\gpt_scripts\s1_cal_deb.xml -t .\deburst\S1A_IW_SLC__1SDV_20180909T003147_20180909T003214_023614_0292B2_753E_Cal_deb_script.dim .\images\S1A_IW_SLC__1SDV_20180909T003147_20180909T003214_023614_0292B2_753E.zip
Powershell Output:
os.getcwd()
'C:\\Users\\Ishack\\Documents\\Neta-Analytics\\Harvest Dates\\S1_SLC_Processing'
The current directory is the same as in PowerShell
I tried something like this:
import subprocess
process = 'gpt .\Method\gpt_scripts\s1_cal_deb.xml -t .\deburst\S1A_IW_SLC__1SDV_20180909T003147_20180909T003214_023614_0292B2_753E_Cal_deb_script.dim .\images\S1A_IW_SLC__1SDV_20180909T003147_20180909T003214_023614_0292B2_753E.zip'
process = subprocess.Popen(['powershell.exe', '-NoProfile', '-Command', '"&{' + process + '}"'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process
Output:
<subprocess.Popen at 0x186bb202388>
But when I press enter I get no response, I would like Python to print out the output just like in Powershell. I researched other similar questions but still no solution to the problem.
Thanks,
Ishack
import os
os.system("powershell.exe -ExecutionPolicy Bypass -File .\SamplePowershell.ps1")
SamplePowershell.ps1
Write-Host "Hello world"
hostname
This worked for me. Commands in file SamplePowershell.ps1.
and if you want to use subprocess
import subprocess
k=subprocess.Popen('powershell.exe hostname', stdout=subprocess.PIPE, stderr=subprocess.PIPE);
print(k.communicate())
If I have a Python script that simply prints the first argument it is given
#!/usr/bin/env python
import sys
try:
print sys.argv[1]
except IndexError:
print "No args"
I can run it find and get the expected output. Now if I write a Bash script that simply runs the python script and echos its output.
#!/bin/bash
test="Test"
echo `python test.py`
Then the bash script will successfully echo "No args". However if I change line 3 to
echo `python test.py $test`
Then I will simply get an empty string as the output of the Bash script and I'm not sure why. I even get the empty string when I change it to
echo `python test.py "test"`
Changing the backticks to $() seems to have worked
You should try in your bash script file:
python test.py $test
It will print your $test variable content.
I call Python script without backquotes and echo. I work a lot with Bash/Python scripts combinations, and this work well for me.
I am using pexpect.run to execute a command. See below:
cmd = "grep -L killed /dir/dumps/*MAC-66.log"
output = pexpect.run(cmd)
When I run this, output equals to:
grep: /dir/dumps/*MAC-66.log: No such file or directory
But when I run the same command in my shell, it works, everytime. I don't see the problem. Any help is appreciated! Does pexpect.run require the command to be split in some fancy way?
Your shell is interpreting the glob, pexpect is not. You could either use python's glob.glob() function to evaluate the glob yourself, or run it through your shell, for example:
cmd = "bash -c 'grep -L killed /dir/dumps/*MAC-66.log'"
Also, if all you're after is output of this command, you ought to check out the subprocess module.