简体   繁体   English

多行字符串EOF的Python subprocess.call

[英]Python subprocess.call with multiline string EOF

I've hit a issue that I don't really understand how to overcome. 我遇到了一个我不太了解如何克服的问题。 I'm trying to create a subprocess in python to run another python script. 我试图在python中创建一个子进程来运行另一个python脚本。 Not too difficult. 不太困难。 The issue is I'm unable to get around is EOF error when a python file includes a super long string. 问题是当python文件包含超长字符串时,我无法解决EOF错误。

Here's an example of what my files look like. 这是我的文件外观的示例。

Subprocess.py: 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

Printer error in terminal 终端中的打印机错误

SyntaxError: EOF while scanning triple-quoted string literal

As a work around, I tried to remove all linebreaks as well as all spaces to see if it was due to it being multi-line. 解决方法是,我尝试删除所有换行符和所有空格,以查看是否是因为它是多行。 That still returned the same result. 那仍然返回相同的结果。

My assumption is that when the subprocess is running and the shell is doing something with the file contents, when the new line is reached the shell itself is freaking out and that's what's terminating the process; 我的假设是,当子进程正在运行并且外壳正在对文件内容进行操作时,当到达新行时,外壳本身就吓坏了,这就是终止进程的原因。 not the file. 不是文件。

What is the correct workaround for having subprocess run a file like this? 子进程运行这样的文件的正确解决方法是什么?

Thank you for your help. 谢谢您的帮助。

Answering my own question here; 在这里回答我自己的问题; my problem was that I didn't file.close() before trying to execute a subprocess.call . 我的问题是,我没有file.close()尝试执行之前subprocess.call

If you encounter this problem, and are working with recently written files this could be your issue too. 如果您遇到此问题,并且正在使用最近写入的文件,那么这也可能是您的问题。 Thank you to everyone who read or responded to this thread. 感谢所有阅读或回复此主题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM