There are several ways to do this:
A simple way is using the os module:
import os
os.system("ls -l")
More complex things can be achieved with the subprocess module: for example:
import subprocess
test = subprocess.Popen(["ping","-W","2","-c", "1", "192.168.1.70"], stdout=subprocess.PIPE)
output = test.communicate()[0]
Answer from Uku Loskit on Stack OverflowHow to run Python Terminal
Making python program a terminal command?
Running Terminal Commands With Python
Python's many command-line utilities
Videos
There are several ways to do this:
A simple way is using the os module:
import os
os.system("ls -l")
More complex things can be achieved with the subprocess module: for example:
import subprocess
test = subprocess.Popen(["ping","-W","2","-c", "1", "192.168.1.70"], stdout=subprocess.PIPE)
output = test.communicate()[0]
I prefer usage of subprocess module:
from subprocess import call
call(["ls", "-l"])
Reason is that if you want to pass some variable in the script this gives very easy way for example take the following part of the code
abc = a.c
call(["vim", abc])
I want to make my python project a command that the user can call in there command prompt / terminal, similar to how you do gcc (Arguments), or python3 (Arguments), but with my project.
Example:
bannana new bannana build bannana add C:/Users/Bob
Would this just be an executable that allows for input arguments, and is in the pcs path so it can be called from the terminal/cmd?
I was wondering if there is a way to run terminal commands(ios) with python. Looking to change file names and other tasks that can be done in terminal ie "mv ~/Desktop/MyFile.rtf ~/Desktop/MyFile-old.rtf". Let me know!
Python 3.12 comes bundled with 50 command-line tools.
For example, python -m webbrowser http://example.com opens a web browser, python -m sqlite3 launches a sqlite prompt, and python -m ast my_file.py shows the abstract syntax tree for a given Python file.
I've dug into each of them and categorized them based on their purpose and how useful they are.
Python's many command-line tools