0

我正在使用以下方法通过 Python 运行 TreeTagger(我知道有一个 Wrapper,但我尝试自己做)subprocess.call()

def call_treetagger(path_file, path_treetagger, language):
# Move the file with one word per line into the TreeTagger folder
source = path_file
destination = path_treetagger + '/swahili_one_word_per_line_tt.txt'
shutil.move(source, destination)
# call TreeTagger via cmd and run it with the moved file as input and in the selected language
cmd1 = 'cd ' + path_treetagger
subprocess.run(cmd1, shell=True)
if language == "sw":
    tt_lang = 'tag-swahili'
if language == "en":
    tt_lang = 'tag-english'
else:
    tt_lang = 'tag-english'
cmd_tt = tt_lang + ' swahili_one_word_per_line_tt.txt' + ' call_via_python_results.txt'
subprocess.run(cmd_tt, shell=True)

call_treetagger("C:/Users/rosas/swahili_one_word_per_line.txt", "C:/TreeTagger", "sw")

无论我使用subprocess.call()还是system.os()它总是说:

Can't open swahili_one_word_per_line_tt.txt: No such file or directory at C:\TreeTagger\cmd\utf8-tokenize.perl line 86.
    reading parameters ...
    tagging ...
     finished.

但是当我通过 Windows Shell 在同一个文件上运行 TreeTagger 时,一切正常。很明显,在 TreeTagger 的 Pearl Script 中找不到这样的文件。swahili_one_word_per_line_tt.txt无论如何,当我通过 Python 调用它时,它为什么要在这个目录中搜索文件?

4

1 回答 1

0

好的,我刚刚找到了解决方案。当我给出整个路径而不仅仅是文件名时:

cmd_tt = tt_lang + ' C:/TreeTagger/swahili_one_word_per_line_tt.txt' + ' C:/TreeTagger/call_via_python_results.txt'

一切正常。我不知道为什么我必须在 PYthon 和 Windows Shell 中以不同的方式执行此操作,但它确实有效。无论如何谢谢你:D

于 2020-04-19T14:00:18.017 回答