Python os 模块,O_DIRECTORY 实例源码
我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用os.O_DIRECTORY。
def mkdir(self, name=None):
if name is None:
os.makedirs(self.url.path, exist_ok=True)
return self
else:
fd = os.open(self.url.path, os.O_DIRECTORY)
os.mkdir(name, dir_fd=fd)
os.close(fd)
return self.join(name)
def render_template(cls, source_file, target_file, binding):
log.info('Rendering %s from template %s', target_file, source_file)
with open(source_file, "r") as handle:
template = handle.read()
assert len(template) > 0, 'Empty template %s' % source_file
with open(target_file, "w") as handle:
handle.write(template.format(**binding))
handle.flush()
os.fsync(handle)
# fsync directory for durability
# https://blog.gocept.com/2013/07/15/reliable-file-updates-with-python/
dirfd = os.open(os.path.dirname(target_file), os.O_DIRECTORY)
os.fsync(dirfd)
os.close(dirfd)