简体   繁体   English

写入临时文件并从命令行读取

[英]Write to temp file and read from command line

I need to write a temporary file to an*x machine using python3 so that I can read it from the command line. 我需要使用python3将一个临时文件写入an * x机器,以便可以从命令行读取它。

import tempfile
import subprocess
from os import path

string = 'hi *there*'

# run markdown server-side
tfile = tempfile.NamedTemporaryFile(mode='w+', suffix='.txt', prefix='prove-math-')
tfile.write(string)
fpath = tfile.name
markdown_path = path.join(LIB_DIR, 'Markdown.pl')
command = [markdown_path, fpath]
completed_process = subprocess.run(command, check=True, stdout=subprocess.PIPE)
string = completed_process.stdout.decode()
tfile.close()

print(string)

The output should be '<p>hi <em>there</em></p>' , but the actual output is '\\n' , which suggests to me that Markdown.pl read the contents of the file as '\\n' . 输出应为'<p>hi <em>there</em></p>' ,但实际输出为'\\n' ,这向我建议Markdown.pl将文件内容读取为'\\n'

Use, 采用,

file_obj.flush() file_obj.flush()

In your case, you'll have to use 就您而言,您必须使用

tfile.flush()

It'll write to the file on being called upon! 它将在被调用时写入文件!

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

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