簡體   English   中英

多行字符串EOF的Python subprocess.call

[英]Python subprocess.call with multiline string EOF

我遇到了一個我不太了解如何克服的問題。 我試圖在python中創建一個子進程來運行另一個python腳本。 不太困難。 問題是當python文件包含超長字符串時,我無法解決EOF錯誤。

這是我的文件外觀的示例。

Subprocess.py:

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

長篇

### 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

解決方法是,我嘗試刪除所有換行符和所有空格,以查看是否是因為它是多行。 那仍然返回相同的結果。

我的假設是,當子進程正在運行並且外殼正在對文件內容進行操作時,當到達新行時,外殼本身就嚇壞了,這就是終止進程的原因。 不是文件。

子進程運行這樣的文件的正確解決方法是什么?

謝謝您的幫助。

在這里回答我自己的問題; 我的問題是,我沒有file.close()嘗試執行之前subprocess.call

如果您遇到此問題,並且正在使用最近寫入的文件,那么這也可能是您的問題。 感謝所有閱讀或回復此主題的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM