your_script.bat:
set VAR_1=this
set VAR_2=that
python your_script.py %1 %VAR_1% %VAR_2%
Answer from Tim Henigan on Stack Overflowyour_script.bat:
set VAR_1=this
set VAR_2=that
python your_script.py %1 %VAR_1% %VAR_2%
Another option is to write arguments right after the python script, following the example:
python your_script.py this that
If you are using Linux .sh file, remember to run dos2unix XXX.sh before you run:
bash XXX.sh.
The reason, in a simple version, is that dos and unix use different newline breakers.
Videos
c:\python27\python.exe c:\somescript.py %*
Open a command line (โ Win+R, cmd, โต Enter)
and type python -V, โต Enter.
You should get a response back, something like Python 2.7.1.
If you do not, you may not have Python installed. Fix this first.
Once you have Python, your batch file should look like
@echo off
python c:\somescript.py %*
pause
This will keep the command window open after the script finishes, so you can see any errors or messages. Once you are happy with it you can remove the 'pause' line and the command window will close automatically when finished.
Python is similar.
import os
os.system("run-client.bat param1 param2")
If you need asynchronous behavior or redirected standard streams.
from subprocess import *
p = Popen(['run-client.bat', param1, param2], stdout=PIPE, stderr=PIPE)
output, errors = p.communicate()
p.wait() # wait for process to terminate
In F#, you could use the Process class from the System.Diagnostics namespace. The simplest way to run the command should be this:
open System.Diagnostics
Process.Start("run-client.bat", "param1 param2")
However, if you need to provide more parameters, you may need to create ProcessStartInfo object first (it allows you to specify more options).
Replace test with "${filename%.faa}" to get the name of the file with .faa removed. You should also quote "${filename}" to avoid problems in case of filenames with spaces.
#!/bin/sh
for filename in *.faa ; do
python predict_genome.py \
--fasta_path /Users/mvalvano/DeepSecE/myruns/"${filename}" \
--model_location /Users/mvalvano/DeepSecE/model/checkpoint.pt \
--data_dir data \
--out_dir myruns/"${filename%.faa}" \
--save_attn --no_cuda
done
exit 0
With input files
bar.faa
foo.faa
the script will run
python predict_genome.py --fasta_path /Users/mvalvano/DeepSecE/myruns/bar.faa --model_location /Users/mvalvano/DeepSecE/model/checkpoint.pt --data_dir data --out_dir myruns/bar --save_attn --no_cuda
python predict_genome.py --fasta_path /Users/mvalvano/DeepSecE/myruns/foo.faa --model_location /Users/mvalvano/DeepSecE/model/checkpoint.pt --data_dir data --out_dir myruns/foo --save_attn --no_cuda
Possible problems with this script:
Since you specify --fasta_path /Users/mvalvano/DeepSecE/myruns/"${filename}", your script will only work without error if the current directory is /Users/mvalvano/DeepSecE/myruns/ or if this directory contains at least the same set of *.faa files as the current directory. (*.faa will expand to the file names in the current directory.)
When /Users/mvalvano/DeepSecE/myruns/ is the current directory, the argument --out_dir myruns/foo might expect or create a directory /Users/mvalvano/DeepSecE/myruns/myruns/foo with double myruns.
Maybe it would make more sense to write it as:
#! /bin/zsh -
topdir=/Users/mvalvano/DeepSecE
ret=0
for filename in $topdir/myruns/*.faa(N); do
outdir=$filename:r
mkdir -p -- $outdir &&
python -- $topdir/predict_genome.py \
--fasta_path $filename \
--model_location $topdir/model/checkpoint.pt \
--data_dir $topdir/data \
--out_dir $outdir \
--save_attn --no_cuda || ret=$?
done
exit $ret
Where we use only absolute paths removing the doubt about what relative paths are relative to.
(here switching to zsh (since that /Users suggests macos) for its :rootname modifier (from csh), its Nullglob qualifier and to remove the need to quote all expansions).