简体   繁体   中英

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. Not too difficult. The issue is I'm unable to get around is EOF error when a python file includes a super long string.

Here's an example of what my files look like.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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