Python 从临时文件读取不成功
Python reading from tempfile not successful
with tempfile.NamedTemporaryFile(delete = False) as tmpfile:
subprocess.call(editor + [tmpfile.name]) # editor = 'subl -w -n' for example
tmpfile.seek(0)
print tmpfile.read()
... 打开我的 Sublime Text 但是当我输入内容并关闭文件时,我没有输出也没有错误,只有一个空行(在 Python 2 上)。是的,程序会等到我写完。
编辑:
我刚刚发现这可能是 Sublime Text
特有的问题,因为 vi
、emacs
和 nano
在作为编辑器输入时都工作正常。
但我仍然想知道如何解决这个问题。
根据 "edit" 部分,您可以保存文件然后重新打开它,这可能不是最好的解决方案,也不是 "solve" 原来的问题,但在至少它应该有效:
import subprocess
import tempfile
editor = ['gedit']
with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
subprocess.call(editor + [tmpfile.name]) # editor = 'subl -w -n' for example
tmpfile.file.close()
tmpfile = file(tmpfile.name)
print tmpfile.read()
如果直接写入输出文件,则按原样工作:
#!/usr/bin/env python
import subprocess
import sys
import tempfile
editor = [sys.executable, '-c', "import sys;"
"open(sys.argv[1], 'w').write('abc')"]
with tempfile.NamedTemporaryFile() as file:
subprocess.check_call(editor + [file.name])
file.seek(0)
print file.read() # print 'abc'
编辑器先写入自己的临时文件,最后重命名失败:
#!/usr/bin/env python
import subprocess
import sys
import tempfile
editor = [sys.executable, '-c', r"""import os, sys, tempfile
output_path = sys.argv[1]
with tempfile.NamedTemporaryFile(dir=os.path.dirname(output_path),
delete=False) as file:
file.write(b'renamed')
os.rename(file.name, output_path)
"""]
with tempfile.NamedTemporaryFile() as file:
subprocess.check_call(editor + [file.name])
file.seek(0)
print file.read() #XXX it prints nothing (expected 'renamed')
重新打开文件 帮助:
#!/usr/bin/env python
import os
import subprocess
import sys
import tempfile
editor = [sys.executable, '-c', r"""import os, sys, tempfile
output_path = sys.argv[1]
with tempfile.NamedTemporaryFile(dir=os.path.dirname(output_path),
delete=False) as file:
file.write(b'renamed')
os.rename(file.name, output_path)
"""]
try:
with tempfile.NamedTemporaryFile(delete=False) as file:
subprocess.check_call(editor + [file.name])
with open(file.name) as file:
print file.read() # print 'renamed'
finally:
os.remove(file.name)
with tempfile.NamedTemporaryFile(delete = False) as tmpfile:
subprocess.call(editor + [tmpfile.name]) # editor = 'subl -w -n' for example
tmpfile.seek(0)
print tmpfile.read()
... 打开我的 Sublime Text 但是当我输入内容并关闭文件时,我没有输出也没有错误,只有一个空行(在 Python 2 上)。是的,程序会等到我写完。
编辑:
我刚刚发现这可能是 Sublime Text
特有的问题,因为 vi
、emacs
和 nano
在作为编辑器输入时都工作正常。
但我仍然想知道如何解决这个问题。
根据 "edit" 部分,您可以保存文件然后重新打开它,这可能不是最好的解决方案,也不是 "solve" 原来的问题,但在至少它应该有效:
import subprocess
import tempfile
editor = ['gedit']
with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
subprocess.call(editor + [tmpfile.name]) # editor = 'subl -w -n' for example
tmpfile.file.close()
tmpfile = file(tmpfile.name)
print tmpfile.read()
如果直接写入输出文件,则按原样工作:
#!/usr/bin/env python
import subprocess
import sys
import tempfile
editor = [sys.executable, '-c', "import sys;"
"open(sys.argv[1], 'w').write('abc')"]
with tempfile.NamedTemporaryFile() as file:
subprocess.check_call(editor + [file.name])
file.seek(0)
print file.read() # print 'abc'
编辑器先写入自己的临时文件,最后重命名失败:
#!/usr/bin/env python
import subprocess
import sys
import tempfile
editor = [sys.executable, '-c', r"""import os, sys, tempfile
output_path = sys.argv[1]
with tempfile.NamedTemporaryFile(dir=os.path.dirname(output_path),
delete=False) as file:
file.write(b'renamed')
os.rename(file.name, output_path)
"""]
with tempfile.NamedTemporaryFile() as file:
subprocess.check_call(editor + [file.name])
file.seek(0)
print file.read() #XXX it prints nothing (expected 'renamed')
重新打开文件
#!/usr/bin/env python
import os
import subprocess
import sys
import tempfile
editor = [sys.executable, '-c', r"""import os, sys, tempfile
output_path = sys.argv[1]
with tempfile.NamedTemporaryFile(dir=os.path.dirname(output_path),
delete=False) as file:
file.write(b'renamed')
os.rename(file.name, output_path)
"""]
try:
with tempfile.NamedTemporaryFile(delete=False) as file:
subprocess.check_call(editor + [file.name])
with open(file.name) as file:
print file.read() # print 'renamed'
finally:
os.remove(file.name)