Call system command 'history' in Linux [duplicate] - python

This question already has answers here:
Run BASH built-in commands in Python?
(2 answers)
Closed 8 years ago.
I am trying to using os.system function to call command 'history'
but the stdout just show that 'sh :1 history: not found'
Other example i.e. os.system('ls') is works. Can anyone can tell me why 'history' does not work, and how to call 'history' command in Python script.

history is not an executable file, but a built-in bash command. You can't run it with os.system.

Related

Windows: Unable to run a shell script in spyder (python 3.8). Invalid Syntax error [duplicate]

This question already has answers here:
How to run .sh on Windows Command Prompt?
(11 answers)
Why does "pip install" inside Python raise a SyntaxError?
(7 answers)
Closed 10 months ago.
I am trying to run a shell script in Spyder (Python 3.8).
I have tried the following and all this is giving me the same error - Invalid Syntax
bash ./filename.sh
bash filename.sh
sh filename.sh
sh ./filename.sh
shell ./filename.sh
shell filename.sh
import subprocess
subprocess.run(.\filename.sh)
source filename.sh
9.
import subprocess
subprocess.run(filename.sh)
The last one gives the error:
name 'filename' doesn't exist.
Note: I have rechecked my pwd.
Add quotes
subprocess.run("filename.sh")
Also, I would like to point out that unless you are using some package to add the syntax bash, sh, etc... you would get the syntax error since Python doesn't know what those keywords mean.

Using pytest from a BAT file [duplicate]

This question already has answers here:
How to start Python virtual environment in .bat batch file?
(2 answers)
Closed 2 years ago.
I am trying to using pytest from a BAT file. I have tried something like this
..\venv\Scripts\activate.bat
pytest -v src/datasets_test.py::TestDatasetsOperations::test_dataset_add
But it seems to do nothing. How can I make it work?
EDIT: the question is similar, but no one will go to other question if he thinks the error is with pytest
In order to call another script from a batch script, you need to use call.
call ..\venv\Scripts\activate.bat
pytest -v src/datasets_test.py::TestDatasetsOperations::test_dataset_add

how to run command line python scripts within my code [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 2 years ago.
there are some scripts that you have to run them from command line like
python run.py -u alex__a -p 123 -v -f
I want to work with them inside my code I don't know whats the name of them so I wasn't able to google about my question . sorry if it't so simple
You are looking for sys.argv to access them. Here is a tutorial:
https://www.pythonforbeginners.com/system/python-sys-argv
If you want to get more serious and use a package to do this more elegantly, you should research the argparse package which is excellent and very intuitive to use.

How to keep a terminal opened by Python alive? [duplicate]

This question already has answers here:
How to keep a Python script output window open?
(27 answers)
Closed 3 years ago.
In my Python script, I create a .bat file (many actually), and I run them via
os.startfile(blah)
Everything works like expected, however, those terminals die after finishing. I want to keep them open, so that I can type more commands manually in those opened terminals.
How?
You could try using the cmd command to run the batch file, and then use command line arguments for cmd to modify its behavior. For example:
os.startfile("cmd.exe /k blah.bat")
Documentation of the available command line arguments can be found here:
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cmd

Run Rsync from Python [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 9 years ago.
I need to run an rsync command from Python. Is this possible and if so, how do I do it?
rsync -Ccavz --delete DJStatic username#website
You can call a subprocess from python using the following snippet
import subprocess
subprocess.call(["ls", "-l"])
In your case, it would be something like this
subprocess.call(["rsync", "-Ccavz", "--delete","DJStatic", "username#website"])
See here for more details.

Categories