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?
CodePudding user response:
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:
import subprocess
command1 = "cd /mnt/d"
command2 = "ls"
subprocess.run(f"{command1} && {command2}", shell=True, check=True)
CodePudding user response:
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 such cd /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.