I was hoping for some advice on using the subprocess module.
I'm trying to run a bash job within a python script so my bash command (in the right directory) is: ./program myjob.inp
This is just running the executable "program" with myjob.inp being the input file (and my python script constantly updates myjob.inp).
I know that if I just wanted to run "program", I could do something like:
with open("tmp.dat","w") as fstore_tmp:
subprocess.call(["./program"], stdout = fstore_tmp)
However, I can't figure out how to run the job taking in the input file myjob.inp such that it's doing the equivalent of ./program myjob.inp. I tried:
with open("tmp.dat","w") as fstore_tmp:
subprocess.call(["./program", "myjob.inp"], stdout = fstore_tmp)
However, that doesn't seem to be working. Does anyone have any suggestions? Thanks!
Related
I am trying to log my python output using bash command "script". The reason that I am doing this is because there is a piece of information that I needed from the program that I am running. But it is a Nonetype which was just printed in the console. I need that piece of information, but there is no way pulling this info out besides manually copy and paste it to my output file.
So I thought I can use "script" in bash to log everything in the console to a txt file and from there I kinda cheated and converted this Nonetype output in the console to a txt file. But now I am trying to make this process automatic. So I started testing this idea out, and I came across this issue: once the script reaches the line to call the "script" command, it spawns a new shell, so it was not able to reach the rest of the code.
Below is a piece of code for testing purpose.
from subprocess import call
call(['script', 'test.txt']) # call script in the bash shell
print('Hello World') # using python to print hello world
call(['exit']) # logout the shell
I am wondering is there any way that can call "script" command and return to the rest of the script?
I am currently using crontab to run a SH script at boot which navigates to the path of my python script, switches to a different python environment and runs my python script, although it works perfectly fine it runs hidden without a terminal for me to monitor whatever the python interpreter prints like errors, how could I make it so the python interpreter points at a newly opened terminal window?
Here is my SH script (runs with the bash interpreter, not sh):
#!/bin/sh
cd /
cd /home/pi/Desktop/Juvia-py
source defenv/bin/activate
python3 juvia.py &
and my crontab entry:
#reboot bash /home/pi/launcher.sh
Thank you
If you just want to record errors, you could pipe STDOUT and STDERR to files, something like
python3 juvia.py >stdout.log 2>stderr.log &
But if you wanted to open it in a separate window so you could interact you would need to manage STDIN more creatively.
I am trying to run a batch file that will open the CMD and start a python script. I nee Windows 10 to start this at 4 am in the morning and know I have to use the Task Scheduler.
My problems seems to be that when I start the batch file the CMD opens up and goes to the correct directory where the python script is but nothing happens.
I am calling the python script from my batch files as "python file.py". when I manual type this into the CMD everything works perfectly but I can not figure out how to start the python scrip from within the batch file to be executed within the CMD.
starts cmd /k "c: && cd\users\ME\Documents"
python file.py
Any help is appreciated.
Thanks
Whenever I run python scripts using batch files I format it like this:
"C:\Users\...\python.exe" "C:\Users...\file.py"
The first command is the location of your python executionable, and the second is the location of the script you want to run.
An optional extra is to include
pause
at the end of your batch file to ensure the cli remains open.
Yesterday I ran into the git execute bit bash script quirk - the one that requires:
git update-index --add --chmod=+x scriptname.sh
and it seemed strange to me that it was even possible to get stuck in this situation. (Ie having created a script file that you don't have permission to run).
If I have created a shell script - surely I can run it under the permissions of the shell execute permissions. Why would it need it's own execute permission bit?
My question is: Why does a bash script require an execute bit if a windows batch script can just be executed?
To run a script you have two options in unix like systems. First Option is to use a direct interpreter call with the script as parameter.
# run a bash script
bash test.sh
# run a python scripts
python test.py
The second option is mark your file as executable, with the execute bit and after a call like this ...
# sample bash
./test.sh
# sample python
./test.py
... your system tries to find the right interpreter for you. For this the first line 'shebang' of the script is used.
Bash example:
#!/bin/bash
# points to the installed bash interpreter - bash example
Python example:
#!/usr/bin/python
# points to the installed python interpreter
To your question windows only use the file extension to detect a executable file.
Well, Linux is not Windows. Linux/Unix file systems support the executable bit to distinguish executable from pure data files, and to control exec permissions for user|group|others. You can still run the script if you prefix it with the name of the shell/binary you want to start it with, but if you want to do ./scriptname.sh or execute it from the path it needs to be flagged as executable for you as the onwer|a group member|some other user, and for scripts usually the shebang in the first line that defines the interpreter to start the script with: #!/bin/bash.
there is a process running that will write some output to a set of files. I wrote a python script that will make a copy of these output files in another directory. Right now I can simply run the python script when I notice the other process is done. How can I get this script to be run automatically when the other process is done?
I don't have control over the other process's source code. Messing with the source code might make the results file inadmissible, so I'd rather not touch it at all.
I am running Python 2.7.1 in an Ubuntu 11.x machine.
You don't tell much about what is the program running before the Python script, but if it is or you can convert to a shell script, you can use this syntax:
$ first-script.sh && python-script.sh
The && operator means that if the first script finished successfully, run the second afterwards.
Of course, you could invoke the python interpreter with your script directly as the 2nd script. Here I assume that it is wrapped in a Bash script.