In order to automate a workflow in Python there are a few Cshell scripts which I need to run. I would like to be able to call these scripts from my Python script itself in Windows. Below are the two commands I need to execute in my Ubuntu 20.04 WSL environment:
command1 = "cd /mnt/Projecten/Bodemdaling/Working_directories/Hawaii/asc/data"
command2 = "organize_files_tops_linux.csh SAFE_filelist ../reframed/pins.ll 1 &> oft_mode1.log &"
The first command navigates to a POSIX-path. The second command runs the Cshell script from that particular path.
I am struggling to find a way to do this. I tried the following code:
import os
os.system('wsl ~ -e sh -c "cd ../../mnt/d/Projecten/Bodemdaling/Working_directories/Hawaii/asc/data; organize_files_tops_linux.csh SAFE_filelist ../reframed/pins.ll 1 &> oft_mode1.log &"')
This does not give me any results yet. What would be an efficient way to perform this task?
EDIT: I have created a helpscript called subsidence_script_1.sh as aqua suggested, containing the following lines of code:
#!/bin/csh
cd /mnt/d/Projecten/Bodemdaling/Working_directories/Hawaii/asc/data
organize_files_tops_linux.csh SAFE_filelist ../reframed/pins.ll 1 &> oft_mode1.log &
In my Python script I am calling this as follows:
import os
os.system('wsl ~ -e sh -c "dash subsidence_script_1.sh"')
The result is that an empty oft_mode1 log is created (the python script does locate the shell script and seems to execute it), but nothing in the actual program that is called in the shell script (organize_files_tops_linux.csh) is run. Is there something that goes wrong here?
And is there a way to run this script and print the shell output in real time in the Spyder Kernel?
2
Answers
If I understand correctly, you would like to run a chain of commands from your WSL environment to navigate to a directory in your windows system and then execute a Cshell script there.
For running commands in your shell you can check out the python’s built-in subprocess module. There are many options for running commands, but it is important to note that every process runs on its own, so chaining two commands with different processes will not work.
You can try the
&&
operator in bash to chain commands together in one process using your local default shell. I do not have your files available, so I tried the following to test, feel free to substitute your own commands:In your Ubuntu session when using WSL,
mnt
is going to be under your root directory/
, so the path you’re giving to WSL should at a minimum start as suchcd /mnt/d/...
However, if your two commands can run in C-Shell, why not create a C-Shell script to contain them, and then launch that script as part of your WSL start invocation?
If you’re trying to call something from Windows within a WSL environment, make it as easy as possible to do that call, even if it means creating a helper script in your WSL Ubuntu environment.