What is the fastest way to execute commands in python? [duplicate] - python

This question already has answers here:
Speed difference betwen subprocess.call() and os.system()
(1 answer)
fastest way to invoke a process from python?
(1 answer)
Closed 11 months ago.
I'm writing a Python script and I need to call some commands, for example creating directories, deleting files, execute docker commands, git commands, make command etc.
What is the fastest way to invoke such commands?
Should I use os.system or subprocess?
The Python script will run on Windows and Linux.

Related

How do I input data into a command prompt from within python 2.7 [duplicate]

This question already has answers here:
Running windows shell commands with python
(7 answers)
Closed 4 years ago.
I want to open a cmd and then input data into the command line from python. I plan to call PEST calibration software to open from within python and I want to start by opening a cmd.
I am using Python 2.7 and so subprocess doesn't seem to work. I have tried os.system('cmd') and I can open the prompt but I can't input any data.
import os
os.system('cmd')
You should be able to pass the exact resulting string to os.system(). Ex:
os.system('notepad.exe')
In other words, os.system behaves the same way a console would.

Run python-script from CMD - windows [duplicate]

This question already has answers here:
How to execute Python scripts in Windows?
(9 answers)
Closed 6 years ago.
I want to run my python script without the python keyword at the beginning.
Example :
I don't want python script.py.
I want script.py
The problem is that when I run it how I want the script opens in a text editor, and it doesn't run in the console...
Why?
I just had to set the default opening of the file with python.exe,
By default it was with VS Code.

Using arguments in python [duplicate]

This question already has answers here:
How to read/process command line arguments?
(22 answers)
Closed 8 years ago.
I wrote a code using Python in Geany within windows and I'm using arguments in the code. I execute the program within the Geany so I don't know how to use arguments.
How could I convert the program to be be run as a standalone system, not to be run from within an IDE.
how could I use terminal in windows to run the code like this :
John~/home/args -> ./test.py -h
In Notepad, write what you would in terminal...
python test.py -h
Save it as a .bat file... Then you can run the bat file :)
PS. This goes for anything you wish to run in CMD

Python: Is it possible to setup a folder trigger? [duplicate]

This question already has answers here:
How do I watch a file for changes?
(28 answers)
Closed 8 years ago.
I would like to monitor the directory using python. Whenever there is a new file the program will notify the user.
Current I am using a loop, which run os.listdir, to poll the directory regularly. However this is very inefficient. Is there any way that I could setup some software trigger (in Python) to enhance the efficiency?
Thanks in advance
See http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html for Windows and http://sourceforge.net/projects/python-fam/ for Linux. Also, you can check out https://github.com/gorakhargosh/watchdog/, which is multi-platform.

how to run my own external command in python script [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Run a linux system command as a superuser, using a python script
(6 answers)
Closed 9 years ago.
I wanna to run my own non-system external commands in python.
Such as "sudo insteon on 23". Subprocess and os.system are designed for system calls.
Does anybody know how to do it?
Thanks
You can use subprocess.Popen for this:
import shlex
import subprocess
proc = subprocess.Popen(shlex.split('sudo insteon on 23'))
proc.communicate()

Categories

Resources