How can I use a Python file to execute terminal command? [duplicate] - python

This question already has answers here:
How can I make one python file run another? [duplicate]
(8 answers)
Closed 2 years ago.
So, all I want is opening a Python script with a Python script? I want the equivalent of 'Python script.py'. Can someone explain how can I execute a. py file. How can I use subprocess to do that? Thanks in advance

With the subprocess module:
import subprocess
subprocess.run(['python', 'script.py'])
If you want to use the same python as the caller you can do:
import subprocess
import sys
subprocess.run([sys.executable, 'script.py'])

There are several ways:
subprocess.call(command)
subprocess.check_call(command)
subprocess.check_output(command)
subprocess.run(command)
os.system(command)
(Each of them has it's own features and you can search in SO to find their differences.
I usually use os.system(command))
Also I found THIS: os.exec*** are realy helpful in these situations

Related

How to run shell-commands in Python? [duplicate]

This question already has answers here:
How to run Python's subprocess and leave it in background
(2 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I made a remote-control client which can receive commands from the server. The commands that are received will be executed as normal cmd commands in a shell. But how can I execute these commands in the background so that the user wont see any in- or output.
For example when I do this, the user would see anything whats going on:
import os
os.system(command_from_server)
You can using subprocess Popen to start a cmd without waiting for end:
from subprocess import Popen
pid = Popen(["ls", "-l"]).pid
Popen has a lot of configure options for handling stdout and stderr. See the ufficial doc.
To execute a command in background, you have to use the subprocess module.
For example:
import subprocess
subprocess.Popen("command", shell=True)
If you want to execute command with more than one argument eg: ls -a, the code is a bit different:
import subprocess
subprocess.Popen("ls -a", shell=True)
To change the directory you can use the os module:
import os
os.chdir(path)
But, as mentioned from tripleee in the comments bellow, you can also pass the cwd parameter to the subprocess.Popen-Method.

How to pass arguments from command line to a main function in python [duplicate]

This question already has answers here:
How to read/process command line arguments?
(22 answers)
Closed 2 years ago.
I've been doing some exercises for AI course and I to get arguments to my code directly from the command line, for example python solution.py resolution_examples/small_example.txt. I now in java you can pass arguments to main fun via command line, but can you do the same thing in python?
sys module has attribute argv which contains all command line parameters.
import sys
print(sys.argv)
For fast handling, that should be enough but when you need a bit more control over cli parsing, standard library has lib called argparse: https://docs.python.org/3/library/argparse.html
Try:
import sys
print(sys.argv)

Execute Shell Script from python and assign output to a variable [duplicate]

This question already has answers here:
Running shell command and capturing the output
(21 answers)
Closed 2 years ago.
I am executing below shell command in python to get the total number of line in a file and was wondering is there any way to assign the output to a variable in python? Thanks
import os
cmd = "wc -l" + " " + /path/to/file/text.txt
num_rows = os.system(cmd)
Try the os.exec class of functions in the os module. They are designed to execute the OS-dependent versions of the utils you're referring to. Shell scripts can also be executed in these processes.
https://docs.python.org/2/library/os.html#process-management
You may also want to try the subprocess module.
https://docs.python.org/2/library/subprocess.html

How to use STDIN from subprocess.Popen [duplicate]

This question already has answers here:
How do I pass a string into subprocess.Popen (using the stdin argument)?
(12 answers)
Closed 6 years ago.
How can I implement something like
$ echo "hello" | my_app
with usage of Python's subprocess?
subprocess.Popen() expects a pipe or a file handle for STDIN. But I want to provide STDIN for the called program via a variable. So something like
myinput = "hello"
subprocess.Popen("an_external_programm", stdin=myinput)
….
I was able to solve my problem by using Popen.communicate()
So some kind of pseudo code:
proc = subprocess.Popen(…)
proc.communicate(input="my_input_via_stdin")
In a python script named myscript.py
import sys
for line in sys.stdin:
print(line)
in unix
echo 'hello' | myscript.py

Execute shell command and retrieve stdout in Python [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Convert POSIX->WIN path, in Cygwin Python, w/o calling cygpath
(4 answers)
Closed 7 years ago.
In Perl, if I want to execute a shell command such as foo, I'll do this:
#!/usr/bin/perl
$stdout = `foo`
In Python I found this very complex solution:
#!/usr/bin/python
import subprocess
p = subprocess.Popen('foo', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = p.stdout.readlines()
retval = p.wait()
Is there any better solution ?
Notice that I don't want to use call or os.system. I would like to place stdout on a variable
An easy way is to use sh package.
some examples:
import sh
print(sh.ls("/"))
# same thing as above
from sh import ls
print(ls("/"))
Read more of the subprocess docs. It has a lot of simplifying helper functions:
output = subprocess.check_output('foo', shell=True, stderr=subprocess.STDOUT)
You can try this
import os
print os.popen('ipconfig').read()
#'ipconfig' is an example of command

Categories