我正在尝试从我的Python代码运行bash脚本。我正在像这样的子过程中调用脚本:
preprocessed = subprocess.check_output([
'bash',
'../Paraphrase_Demo/models/processing_utils/preprocess_data_givenBPE.sh',
phrase, src_lang, tgt_lang, this_dir
])
这是脚本preprocess_data_givenBPE.sh:
#!/usr/bin/env bash
REMOTE_PATH=$4/processing_utils
SCRIPTS=$REMOTE_PATH/tiny-moses
TOKENIZER=$SCRIPTS/tokenizer.perl
DETOKENIZER=$SCRIPTS/detokenizer_v2.perl
CLEAN=$SCRIPTS/clean-corpus-n.perl
NORM_PUNC=$SCRIPTS/normalize-punctuation.perl
REM_NON_PRINT_CHAR=$SCRIPTS/remove-non-printing-char.perl
BPEROOT=$4/zhen/tools/subword-nmt/subword_nmt
text=$1
flan=$2
BPE=$4/$2$3/data/bpecodes
echo $text | perl $NORM_PUNC -l $flan | perl $REM_NON_PRINT_CHAR |
perl $TOKENIZER -a -l $flan -q | python3 $BPEROOT/apply_bpe.py -c $BPE | cat
当我运行Python程序时,得到以下输出:
Can't open perl script "C:/Users/Administrator/source/repos/Paraphrasing/Paraphrase_Demo/models/processing_utils/tiny-moses/normalize-punctuation.perl": No such file or directory
Can't open perl script "C:/Users/Administrator/source/repos/Paraphrasing/Paraphrase_Demo/models/processing_utils/tiny-moses/remove-non-printing-char.perl": No such file or directory
Can't open perl script "C:/Users/Administrator/source/repos/Paraphrasing/Paraphrase_Demo/models/processing_utils/tiny-moses/tokenizer.perl": No such file or directory
python3: can't open file 'C:/Users/Administrator/source/repos/Paraphrasing/Paraphrase_Demo/models/zhen/tools/subword-nmt/subword_nmt/apply_bpe.py': [Errno 2] No such file or directory
我已将所有这些路径复制并粘贴到powershell中,并ls
来验证它们是否正确,但仍然表示找不到它们。这里发生了什么?如果我直接运行bash脚本,也会发生这种情况。
编辑:我尝试在路径中使用反斜杠并收到相同的错误。我还发现了这篇文章:Bash: cannot execute a perl script using an absolute path?,所以我尝试使用Bash脚本目录中的相对路径,但仍然显示“没有这样的文件或目录。”
答案 0 :(得分:0)
经过大量调试,我发现了问题。如果我在Powershell中ls
将列出的路径存在,则在Powershell中键入bash
不仅会打开bash
Shell,它实际上会更改目录结构。我认为这可能与Linux的Windows子系统有关,但结果是,一旦进入C:
外壳,/mnt/c
就会更改为bash
。替换掉所有路径后,我就可以运行脚本了。