2

I need to write a temporary file to a n*x machine using python3 so that I can read it from the command line.

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'.

mareoraft
  • 3,474
  • 4
  • 26
  • 62

1 Answers1

0

Use,

file_obj.flush()

In your case, you'll have to use

tfile.flush()

It'll write to the file on being called upon!

Ubdus Samad
  • 1,218
  • 1
  • 15
  • 27
  • Read the comments, I suggested him to close it as well, but, whatever works for him is the answer my friend! – Ubdus Samad Aug 07 '17 at 07:08
  • 1
    @SergeBallesta I believe that `tfile.close()` doesn't work because `tempfile` deletes the file after closing it. – mareoraft Aug 07 '17 at 16:11
  • 1
    @SergeBallesta Also, this is only helpful information if you list which operating systems will give an issue. The [tempfile](https://docs.python.org/3.5/library/tempfile.html?highlight=tempfile#module-tempfile) docs only mention that an issue will occur with Windows NT or later. – mareoraft Aug 07 '17 at 16:13