本文整理汇总了Python中os.tmpfile方法的典型用法代码示例。如果您正苦于以下问题:Python os.tmpfile方法的具体用法?Python os.tmpfile怎么用?Python os.tmpfile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.tmpfile方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: restore
# 需要导入模块: import os [as 别名]
# 或者: from os import tmpfile [as 别名]
def restore(self):
"""restore original fd and returns captured output"""
# hack hack hack
self.tmpfile.flush()
try:
ref_file = getattr(sys, '__%s__' % self.attr)
ref_file.flush()
except AttributeError:
pass
if hasattr(self.oldval, 'flush'):
self.oldval.flush()
# restore original file descriptor
os.dup2(self._savefd, self.targetfd)
# restore sys module
setattr(sys, self.attr, self.oldval)
# close backup descriptor
os.close(self._savefd)
# go to beginning of file and read it
self.tmpfile.seek(0)
return self.tmpfile.read()
示例2: test_tmpfile
# 需要导入模块: import os [as 别名]
# 或者: from os import tmpfile [as 别名]
def test_tmpfile(self):
# As with test_tmpnam() below, the Windows implementation of tmpfile()
# attempts to create a file in the root directory of the current drive.
# On Vista and Server 2008, this test will always fail for normal users
# as writing to the root directory requires elevated privileges. With
# XP and below, the semantics of tmpfile() are the same, but the user
# running the test is more likely to have administrative privileges on
# their account already. If that's the case, then os.tmpfile() should
# work. In order to make this test as useful as possible, rather than
# trying to detect Windows versions or whether or not the user has the
# right permissions, just try and create a file in the root directory
# and see if it raises a 'Permission denied' OSError. If it does, then
# test that a subsequent call to os.tmpfile() raises the same error. If
# it doesn't, assume we're on XP or below and the user running the test
# has administrative privileges, and proceed with the test as normal.
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning)
if sys.platform == 'win32':
name = '\\python_test_os_test_tmpfile.txt'
if os.path.exists(name):
os.remove(name)
try:
fp = open(name, 'w')
except IOError, first:
# open() failed, assert tmpfile() fails in the same way.
# Although open() raises an IOError and os.tmpfile() raises an
# OSError(), 'args' will be (13, 'Permission denied') in both
# cases.
try:
fp = os.tmpfile()
except OSError, second:
self.assertEqual(first.args, second.args)
else:
self.fail("expected os.tmpfile() to raise OSError")
return
else:
示例3: test_tmpfile
# 需要导入模块: import os [as 别名]
# 或者: from os import tmpfile [as 别名]
def test_tmpfile(self):
if not hasattr(os, "tmpfile"):
return
# As with test_tmpnam() below, the Windows implementation of tmpfile()
# attempts to create a file in the root directory of the current drive.
# On Vista and Server 2008, this test will always fail for normal users
# as writing to the root directory requires elevated privileges. With
# XP and below, the semantics of tmpfile() are the same, but the user
# running the test is more likely to have administrative privileges on
# their account already. If that's the case, then os.tmpfile() should
# work. In order to make this test as useful as possible, rather than
# trying to detect Windows versions or whether or not the user has the
# right permissions, just try and create a file in the root directory
# and see if it raises a 'Permission denied' OSError. If it does, then
# test that a subsequent call to os.tmpfile() raises the same error. If
# it doesn't, assume we're on XP or below and the user running the test
# has administrative privileges, and proceed with the test as normal.
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning)
if sys.platform == 'win32':
name = '\\python_test_os_test_tmpfile.txt'
if os.path.exists(name):
os.remove(name)
try:
fp = open(name, 'w')
except IOError, first:
# open() failed, assert tmpfile() fails in the same way.
# Although open() raises an IOError and os.tmpfile() raises an
# OSError(), 'args' will be (13, 'Permission denied') in both
# cases.
try:
fp = os.tmpfile()
except OSError, second:
self.assertEqual(first.args, second.args)
else:
self.fail("expected os.tmpfile() to raise OSError")
return
else:
示例4: __init__
# 需要导入模块: import os [as 别名]
# 或者: from os import tmpfile [as 别名]
def __init__(self, fd, attr='stdout', printonly=None):
self.targetfd = fd
self.tmpfile = os.tmpfile() # self.maketempfile()
self.printonly = printonly
# save original file descriptor
self._savefd = os.dup(fd)
# override original file descriptor
os.dup2(self.tmpfile.fileno(), fd)
# also modify sys module directly
self.oldval = getattr(sys, attr)
setattr(sys, attr, self) # self.tmpfile)
self.attr = attr
示例5: write
# 需要导入模块: import os [as 别名]
# 或者: from os import tmpfile [as 别名]
def write(self, msg):
# msg might be composed of several lines
for line in msg.splitlines():
line += '\n' # keepdend=True is not enough
if self.printonly is None or self.printonly.search(line) is None:
self.tmpfile.write(line)
else:
os.write(self._savefd, line)
## def maketempfile(self):
## tmpf = os.tmpfile()
## fd = os.dup(tmpf.fileno())
## newf = os.fdopen(fd, tmpf.mode, 0) # No buffering
## tmpf.close()
## return newf
示例6: test_tmpfile
# 需要导入模块: import os [as 别名]
# 或者: from os import tmpfile [as 别名]
def test_tmpfile(self):
if not hasattr(os, "tmpfile"):
return
# As with test_tmpnam() below, the Windows implementation of tmpfile()
# attempts to create a file in the root directory of the current drive.
# On Vista and Server 2008, this test will always fail for normal users
# as writing to the root directory requires elevated privileges. With
# XP and below, the semantics of tmpfile() are the same, but the user
# running the test is more likely to have administrative privileges on
# their account already. If that's the case, then os.tmpfile() should
# work. In order to make this test as useful as possible, rather than
# trying to detect Windows versions or whether or not the user has the
# right permissions, just try and create a file in the root directory
# and see if it raises a 'Permission denied' OSError. If it does, then
# test that a subsequent call to os.tmpfile() raises the same error. If
# it doesn't, assume we're on XP or below and the user running the test
# has administrative privileges, and proceed with the test as normal.
if sys.platform == 'win32':
name = '\\python_test_os_test_tmpfile.txt'
if os.path.exists(name):
os.remove(name)
try:
fp = open(name, 'w')
except IOError, first:
# open() failed, assert tmpfile() fails in the same way.
# Although open() raises an IOError and os.tmpfile() raises an
# OSError(), 'args' will be (13, 'Permission denied') in both
# cases.
try:
fp = os.tmpfile()
except OSError, second:
self.assertEqual(first.args, second.args)
else:
self.fail("expected os.tmpfile() to raise OSError")
return
示例7: __call__
# 需要导入模块: import os [as 别名]
# 或者: from os import tmpfile [as 别名]
def __call__(self, code=""):
tmpFile = os.tmpfile()
tmpFile.write(code)
tmpFile.seek(0)
self.plugin.msgThread.dll.MceIrPlaybackFromFile(
get_osfhandle(tmpFile.fileno())
)
示例8: Configure
# 需要导入模块: import os [as 别名]
# 或者: from os import tmpfile [as 别名]
def Configure(self, code=""):
panel = eg.ConfigPanel()
code = ' '.join([("%02X" % ord(c)) for c in code])
editCtrl = wx.TextCtrl(panel, -1, code, style=wx.TE_MULTILINE)
font = editCtrl.GetFont()
font.SetFaceName("Courier New")
editCtrl.SetFont(font)
editCtrl.SetMinSize((-1, 100))
def Learn(dummyEvent):
tmpFile = os.tmpfile()
self.plugin.msgThread.dll.MceIrRecordToFile(
get_osfhandle(tmpFile.fileno()),
10000
)
tmpFile.seek(0)
code = tmpFile.read()
tmpFile.close()
editCtrl.SetValue(' '.join([("%02X" % ord(c)) for c in code]))
learnButton = wx.Button(panel, -1, "Learn IR Code")
learnButton.Bind(wx.EVT_BUTTON, Learn)
panel.sizer.Add(editCtrl, 1, wx.EXPAND)
panel.sizer.Add((5, 5))
panel.sizer.Add(learnButton, 0, wx.ALIGN_RIGHT)
while panel.Affirmed():
code = editCtrl.GetValue().replace(" ", "").decode("hex_codec")
panel.SetResult(code)