本文整理汇总了Python中os.supports_fd方法的典型用法代码示例。如果您正苦于以下问题:Python os.supports_fd方法的具体用法?Python os.supports_fd怎么用?Python os.supports_fd使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.supports_fd方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_rmtree_uses_safe_fd_version_if_available
# 需要导入模块: import os [as 别名]
# 或者: from os import supports_fd [as 别名]
def test_rmtree_uses_safe_fd_version_if_available(self):
_use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <=
os.supports_dir_fd and
os.listdir in os.supports_fd and
os.stat in os.supports_follow_symlinks)
if _use_fd_functions:
self.assertTrue(shutil._use_fd_functions)
self.assertTrue(shutil.rmtree.avoids_symlink_attacks)
tmp_dir = self.mkdtemp()
d = os.path.join(tmp_dir, 'a')
os.mkdir(d)
try:
real_rmtree = shutil._rmtree_safe_fd
class Called(Exception): pass
def _raiser(*args, **kwargs):
raise Called
shutil._rmtree_safe_fd = _raiser
self.assertRaises(Called, shutil.rmtree, d)
finally:
shutil._rmtree_safe_fd = real_rmtree
else:
self.assertFalse(shutil._use_fd_functions)
self.assertFalse(shutil.rmtree.avoids_symlink_attacks)
示例2: touch
# 需要导入模块: import os [as 别名]
# 或者: from os import supports_fd [as 别名]
def touch(fname, mode=0o666, dir_fd=None, **kwargs):
## After http://stackoverflow.com/questions/1158076/implement-touch-using-python
if sys.version_info < (3, 3, 0):
with open(fname, 'a'):
os.utime(fname, None) # set to now
else:
flags = os.O_CREAT | os.O_APPEND
with os.fdopen(os.open(fname, flags=flags, mode=mode, dir_fd=dir_fd)) as f:
os.utime(f.fileno() if os.utime in os.supports_fd else fname,
dir_fd=None if os.supports_fd else dir_fd, **kwargs)
示例3: touch
# 需要导入模块: import os [as 别名]
# 或者: from os import supports_fd [as 别名]
def touch(fname, mode=0o666, dir_fd=None, **kwargs):
"""Utility function for updating a file time stamp.
Source:
https://stackoverflow.com/questions/1158076/implement-touch-using-python
"""
flags = os.O_CREAT | os.O_APPEND
with os.fdopen(os.open(fname, flags=flags, mode=mode, dir_fd=dir_fd)) as f:
os.utime(f.fileno() if os.utime in os.supports_fd else fname,
dir_fd=None if os.supports_fd else dir_fd, **kwargs)
示例4: touch
# 需要导入模块: import os [as 别名]
# 或者: from os import supports_fd [as 别名]
def touch(fpath, mode=0o666, dir_fd=None, verbose=0, **kwargs):
"""
change file timestamps
Works like the touch unix utility
Args:
fpath (str | PathLike): name of the file
mode (int): file permissions (python3 and unix only)
dir_fd (file): optional directory file descriptor. If specified, fpath
is interpreted as relative to this descriptor (python 3 only).
verbose (int): verbosity
**kwargs : extra args passed to ``os.utime`` (python 3 only).
Returns:
str: path to the file
References:
https://stackoverflow.com/questions/1158076/implement-touch-using-python
Example:
>>> import ubelt as ub
>>> from os.path import join
>>> dpath = ub.ensure_app_cache_dir('ubelt')
>>> fpath = join(dpath, 'touch_file')
>>> assert not exists(fpath)
>>> ub.touch(fpath)
>>> assert exists(fpath)
>>> os.unlink(fpath)
"""
if verbose:
print('Touching file {}'.format(fpath))
if six.PY2: # nocover
with open(fpath, 'a'):
os.utime(fpath, None)
else:
flags = os.O_CREAT | os.O_APPEND
with os.fdopen(os.open(fpath, flags=flags, mode=mode, dir_fd=dir_fd)) as f:
os.utime(f.fileno() if os.utime in os.supports_fd else fpath,
dir_fd=None if os.supports_fd else dir_fd, **kwargs)
return fpath
示例5: touch_file
# 需要导入模块: import os [as 别名]
# 或者: from os import supports_fd [as 别名]
def touch_file(filename, mode=0o666, times=None, dir_fd=None, ref=None, **kwargs):
'''update/create a sentinel file.
modified from: https://stackoverflow.com/questions/1158076/implement-touch-using-python
Compressed files (ending in .gz) are created as empty 'gzip'
files, i.e., with a header.
'''
flags = os.O_CREAT | os.O_APPEND
existed = os.path.exists(filename)
if filename.endswith(".gz") and not existed:
# this will automatically add a gzip header
with gzip.GzipFile(filename, "w") as fhandle:
pass
if ref:
stattime = os.stat(ref)
times = (stattime.st_atime, stattime.st_mtime)
with os.fdopen(os.open(
filename, flags=flags, mode=mode, dir_fd=dir_fd)) as fhandle:
os.utime(
fhandle.fileno() if os.utime in os.supports_fd else filename,
dir_fd=None if os.supports_fd else dir_fd,
**kwargs)
示例6: touch
# 需要导入模块: import os [as 别名]
# 或者: from os import supports_fd [as 别名]
def touch(fname, mode=0o666, dir_fd=None, **kwargs):
"""
Implementation of coreutils touch
http://stackoverflow.com/a/1160227
"""
flags = os.O_CREAT | os.O_APPEND
with os.fdopen(os.open(fname, flags=flags, mode=mode, dir_fd=dir_fd)) as f:
os.utime(f.fileno() if os.utime in os.supports_fd else fname,
dir_fd=None if os.supports_fd else dir_fd, **kwargs)