我使用subprocess.call()方法通过Python运行TreeTagger (我知道有一个包装器,但我试着自己做):
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脚本中找不到这样的文件。无论如何,当我通过Python调用它时,为什么它在这个目录中搜索文件swahili_one_word_per_line_tt.txt?
发布于 2020-04-19 22:00:18
好吧,我找到了一个解决方案。当我给出整个路径,而不仅仅是文件名liek时:
cmd_tt = tt_lang + ' C:/TreeTagger/swahili_one_word_per_line_tt.txt' + ' C:/TreeTagger/call_via_python_results.txt'一切正常。我不知道为什么我必须在PYthon和Windows Shell中以不同的方式来做这件事,但是它可以工作。无论如何,谢谢你:D
https://stackoverflow.com/questions/61304709
复制相似问题