Python subprocess.call与多行字符串EOF

时间:2016-06-29 02:24:39

标签: python python-2.7 shell subprocess

我遇到了一个我真不懂得克服的问题。我试图在python中创建一个子进程来运行另一个python脚本。不太难。问题是,当python文件包含超长字符串时,我无法解决EOF错误。

以下是我的文件的示例。

Subprocess.py:

### call longstr.py from the primary pyfile
subprocess.call(['python longstr.py'], shell = True)

Longstr.py

### called from subprocess.py
### the actual string is a lot longer; this is an example to illustrate how the string is formatted
lngstr = """here is a really long 
string (which has many n3w line$ and "characters")
that are causing the python file to state the file is ending early
"""
print lngstr

终端中的打印机错误

SyntaxError: EOF while scanning triple-quoted string literal

作为一种解决方法,我尝试删除所有换行符以及所有空格以查看它是否是由于它是多行的。这仍然会返回相同的结果。

我的假设是,当子进程正在运行并且shell正在对文件内容执行某些操作时,当到达新行时,shell本身就会吓坏,这就是终止进程的原因;不是文件。

让子进程运行这样的文件的正确解决方法是什么?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在这里回答我自己的问题;我的问题是在尝试执行file.close()之前我没有subprocess.call

如果您遇到此问题,并且正在使用最近编写的文件,这也可能是您的问题。感谢阅读或回复此主题的所有人。