Reputation: 2031
I'm writing a script which writes it's output to a system directory. Rather than run the entire script using root permissions, I'm writing the output to a temporary file and only using elevated privileges when copying the temporary file to the output.
print(tempfile.read())
return_code = suprocess.call(['/usr/bin/sudo', 'cp', tmpfile.name, outfile])
if return_code != 0:
print('Copy failed')
sys.exit(1)
return_code = subprocess.call(['/usr/bin/sudo', 'chmod', '664', outfile])
with open(outfile) as f2:
print(f2.read())
The first debug prints out the expected contents of the generated file, but the second dprint is empty. In addition, inspecting the file in the filesystem says the file is 0
bytes in length and owned by root.
I've tried the following and it doesn't seem to make a difference:
shell=True
tempfile.NamedTempfile
or creating a temporary file myself and cleaning upUpvotes: 0
Views: 669
Reputation: 2031
Solved the problem. tempfile
was still open for writing when cp
was called and the write hadn't flushed to disk when the copy was attempted.
Upvotes: 1