本文整理汇总了Python中tempfile.NamedTemporaryFile.realname方法的典型用法代码示例。如果您正苦于以下问题:Python NamedTemporaryFile.realname方法的具体用法?Python NamedTemporaryFile.realname怎么用?Python NamedTemporaryFile.realname使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tempfile.NamedTemporaryFile
的用法示例。
在下文中一共展示了NamedTemporaryFile.realname方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: open
# 需要导入模块: from tempfile import NamedTemporaryFile [as 别名]
# 或者: from tempfile.NamedTemporaryFile import realname [as 别名]
def open(self, basefile, maindir, suffix, mode="r", version=None, attachment=None):
"""
Context manager that opens files for reading or
writing. The parameters are the same as for :meth:`~ferenda.DocumentStore.path`, and the
note is applicable here as well -- use
:meth:`~ferenda.DocumentStore.open_downloaded`, :meth:`~ferenda.DocumentStore.open_parsed` et al if
possible.
Example:
>>> store = DocumentStore(datadir="/tmp/base")
>>> with store.open('123/a', 'parsed', '.xhtml', mode="w") as fp:
... res = fp.write("hello world")
>>> os.path.exists("/tmp/base/parsed/123/a.xhtml")
True
"""
filename = self.path(basefile, maindir, suffix, version, attachment)
fp = NamedTemporaryFile(mode, delete=False)
fp.realname = filename
try:
yield fp
finally:
tempname = fp.name
fp.close()
if not os.path.exists(filename) or not filecmp.cmp(tempname, filename):
util.ensure_dir(filename)
shutil.move(tempname, filename)
else:
os.unlink(tempname)
示例2: _open
# 需要导入模块: from tempfile import NamedTemporaryFile [as 别名]
# 或者: from tempfile.NamedTemporaryFile import realname [as 别名]
def _open(self, filename, mode):
if "w" in mode:
fp = NamedTemporaryFile(mode, delete=False)
fp.realname = filename
try:
yield fp
finally:
tempname = fp.name
fp.close()
if not os.path.exists(filename) or not filecmp.cmp(tempname, filename):
util.ensure_dir(filename)
shutil.move(tempname, filename)
else:
os.unlink(tempname)
else:
if "a" in mode and not os.path.exists(filename):
util.ensure_dir(filename)
fp = open(filename, mode)
yield fp