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

时间:2017-08-05 03:14:39

标签: python python-3.x file-writing temp file-read

我需要使用python3将一个临时文件写入n * 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)

输出应为'<p>hi <em>there</em></p>',但实际输出为'\n',这表明我Markdown.pl将文件内容读为'\n'

1 个答案:

答案 0 :(得分:0)

使用,

  

file_obj.flush()

在您的情况下,您必须使用

tfile.flush()

它会在被调用时写入文件!

相关问题