Executing a Powershell command from MATLAB - python

I'm trying to automate my workflow. So I'm generating a test list for my system in MATLAB. In the same script, I'm then waiting for a new log file which only gets generated when I run a python script in a certain directory through windows powershell.
That python script would take the test list generated and produce a new log file. My code looks for the date of modification of that file, if it has changed, it means a new log file has been produced. The code then extracts certain strings of characters and then plot the result.
My question is how can I invoke powershell from MATLAB to invoke that python script execution command in a specific directory?
Regards

If you really want to commit to this Powershell-in-the-middle approach, you can call it with system
system( 'powershell -command "Some Powershell command"' );
You can collect outputs from this if there are any.
Seems like it would be easier to call the Python script from Matlab though and cut out the call to Powershell, this could also be done with the system command (i.e. without also calling Powershell in between). Or you could invoke Python directly.

Related

Python subprocess.call is not running the same as with python shell

I am trying to merge 2 python scripts(before.py and after.py) to one as I need to set a cron job for the python script to run both scripts in one execution.
The 2 scripts are connected wherein the file that is processed by after.py (tmp1.txt) is being generated by before.py.
Original order of script is
python3 before.py someCsv.csv > tmp1.txt
python3 after.py tmp1.txt >tmp2.txt
in before.py, I added this
subprocess.call(' python3 after.py tmp1.txt > tmp2.txt', shell=True)
Which I believe will allow to execute a run a shell command inside python.
If I run the script directly (after.py), it returns 44,623 records.
But if i run after.py within before.py via subprocess.call(), it returns only 44,619 records.
I am using the same file in both scripts ,that is tmp1.txt.
I tried subprocess.call(), subprocess.run(), and os.system(). It seems that I am doing something wrong here.
instead of outputting the result to a new file via ">" in python shell, I just created a new file within before.py which guarantees that before after.py will run, tmp1.txt is 100% completed. Which allows me to get the full 44,623 records.
This is the end of before.py. Instead of directly outputting to a file within the python shell, I just created the file within the script itself.

Bash scripting: How to input values into a seperate running python shell, and write back the result

I'm trying to create a bash script that can write values into a separate python shell that is constantly running, which is expecting user input, then read back the returned value into my bash script.
Backstory: I can't just call the python script from directly within the bash script as the python script is constantly running, with a fairly long initialization which I don't want to do each time I call it.
So it can be condensed into two questions;
How can I write to python 'user input' programmatically within bash
How can I reliably read what is returned from the python script into a bash variable (it can be printed out and returned however I like)
I am familiar with bash basics, but I am still fairly new to it. I am guessing it has something to do with reading/writing to stderr, stdout, or stdin?
Any advice given would be very useful, thanks.
After further searching I found that sockets are definitely the way to go. The example I found here worked perfectly:
Basic Python client socket example
Thank you Shardj for the suggestion

Why running a python file doesn't require the execute permission?

Why running a python file doesn't require the x permission when running it like this:
python script.py
But it does when it's being run as:
./script.py
Because what you are running with python script.py is the python program; then, it loads and runs the script that you specified in parameters, that is script.py (basically a text file). The script file doesn't need to be an executable because what is executed here is the python interpreter (the python binary itself, that should obviously have the x permission).
With .\script.py, you try to run directly your script (still the same text file) as a program. When you do that, you want it to be parsed with the interpreter that you specified in the first line of your script code, the "shebang", e.g. #!/usr/bin/env python. If it's not set with the x permission, the OS doesn't try to "execute" your file (though it might try to open it with the default program, where applicable), so, it will not care about the shebang.
The file itself it interpreted (read) rather than actually executed in your first example. The python application is what needs execute rights.
In the second example the file itself is being executed, so needs those rights in order to proceed.
When we run a script as python script.py we actually invoke the python interpreter which is generally located at /usr/bin/python (The output of which python will tell you where exactly).
The interpreter in turn reads the scripts and executes its code.
It is the interpreter that has the execute permission.
When a script is executed as ./script.py then the script is executed directly and hence the script requires execute permission. The interpreter used is specified by shebang line.
When the kernel finds that the first two bytes are #! then it uses the rest of the line as interpreter and passes the file as argument. Note that to do this the file needs to have execute permission. In the former case we are indirectly doing what the kernel would do had we executed the script as ./script.py
In short for executing by method1 the interpreter needs only read permission but for later it needs to execute it directly

Why os.system() in script fails?

I have a script containing a line of the type:
os.system('file.exe <arg>')
file.exe makes some calculations taking <arg> as input and writes an output file. <arg> is a file containing parameter values located in the same folder as file.exe.
The instruction, if executed independently in Python IDLE, works well. However, when executing the script in IDLE it fails. That is, a Windows shell window flashes, but no output file is written. There is a similar behaviour with os.popen() and subprocess.call(). Everything works well in Spyder on another computer.
I was wondering which might be the reason for this behaviour.

git cannot execute python-script as hook

I have created a little pre-commit hook in python. This hook works like a charm under Linux, but in Windows it keeps telling me:
error: cannot spawn .git/hooks/pre-commit: No such file or directory
I know there have been similar questions here about the same issue and the conclusion seams to be the shebang. My script has this on the very first line:
#!F:\PortableApps\PortablePython3.2\App\python.exe
It's also interesting to note that executing the script simply by writing .git/hooks/pre-commit works wonderful, but as soon as I try to commit, git spits out the above message.
Another interesting thing is, when I convert the encoding from ANSI to UTF-8 (using Notepad++), I get the following error when trying to execute the script:
.git/hooks/pre-commit: Cannot execute binary file
I'm using the following tools:
PortablePython 3.2.1.1
msysgit 1.7.6 (Portable)
I used the proxy-approach to make the python script work under windows (with msysgit). The complete script (with description on how I did it) might be found here: https://gist.github.com/1839424
Here is the important part about making it work under Windows
If you're working with Windows (and "msysgit"), it's a little more complicated. Since "msysgit" seems to have a problem handling the SHEBANG, you'll have to use a little trick to make the script executable (further information on this problem can be found here).
In order to make the script work, you'll want to remove the SHEBANG from the Python script ("pre-commit.py") and use a wrapper bash-script to call the interpreter. This script should look something like this:
#!/bin/sh
python .git/hooks/pre-commit.py
Store this script as a file called "pre-commit" (no file-ending). This assumes that you have Python in your PATH. If you don't, you can also specify the full path to your interpreter-executable.
This script will be called by "git commit" and call the python-script to check for the huge files. The path after the SHEBANG should not be changed, as "msysgit" will remap it automatically. You must specify a path relative to the repo-root for the Python script to be executed (because thats from where the script is called).
Afterwards you'll want to copy both the wrapper-file ("pre-commit") and the Python-script ("pre-commit.py") to your repos ".git/hooks"-directory, personalize the Python-script ("max_file_size" and "git_binary_path") and mark the "pre-commit"-file executable.

Categories

Resources