本文整理汇总了Python中os.html方法的典型用法代码示例。如果您正苦于以下问题:Python os.html方法的具体用法?Python os.html怎么用?Python os.html使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.html方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send_sigint
# 需要导入模块: import os [as 别名]
# 或者: from os import html [as 别名]
def send_sigint(self):
# `signal.CTRL_C_EVENT` is also sent to the test process itself.
# See https://docs.python.org/3.6/library/os.html#os.kill
# So we need to wait the signal and ignore it.
# We can NOT ignore the signal by modifying the signal handler here.
# If we temporary ignores the signal, the signal will sent again
# when the signal handler is restored.
# If we ignore the signal permanently, we couldn't interrupt the test.
if os.name == 'nt':
try:
os.kill(self.p.pid, signal.CTRL_C_EVENT)
while True:
pass
except KeyboardInterrupt:
pass
else:
os.kill(self.p.pid, signal.SIGINT)
示例2: run
# 需要导入模块: import os [as 别名]
# 或者: from os import html [as 别名]
def run(command):
# TODO: replace this with fork()
# (https://docs.python.org/2/library/os.html#os.fork)
pid = 0
if pid == 0:
# This is the child, we'll try to do some containment here
try:
contain(command)
except Exception:
traceback.print_exc()
os._exit(1) # something went wrong in contain()
# This is the parent, pid contains the PID of the forked process
# wait for the forked child and fetch the exit status
_, status = os.waitpid(pid, 0)
print('{} exited with status {}'.format(pid, status))
示例3: rename
# 需要导入模块: import os [as 别名]
# 或者: from os import html [as 别名]
def rename(src, dst):
"""os.rename(src, dst) wrapper with support for long paths on Windows.
Availability: Unix, Windows."""
if isWindows():
# On Windows, rename fails if destination exists, see
# https://docs.python.org/2/library/os.html#os.rename
try:
os.rename(_makelongpath(src), _makelongpath(dst))
except OSError as e:
if e.errno == errno.EEXIST:
os.remove(_makelongpath(dst))
os.rename(_makelongpath(src), _makelongpath(dst))
else:
raise
else:
os.rename(src, dst)
示例4: system
# 需要导入模块: import os [as 别名]
# 或者: from os import html [as 别名]
def system():
import platform
# On some Windows installation (Python 2.4) platform.system() is
# broken and incorrectly returns 'Microsoft' instead of 'Windows'.
# http://mail.python.org/pipermail/patches/2007-June/022947.html
syst = platform.system()
if syst == 'Microsoft':
return 'Windows'
return syst
# Set and get environment variables does not handle unicode strings correctly
# on Windows.
# Acting on os.environ instead of using getenv()/setenv()/unsetenv(),
# as suggested in <http://docs.python.org/library/os.html#os.environ>:
# "Calling putenv() directly does not change os.environ, so it's
# better to modify os.environ." (Same for unsetenv.)
示例5: test_set_config_value_file_permissions
# 需要导入模块: import os [as 别名]
# 或者: from os import html [as 别名]
def test_set_config_value_file_permissions(self):
self.cli_config.set_value('test_section', 'test_option', 'a_value')
file_mode = os.stat(self.cli_config.config_path).st_mode
self.assertTrue(bool(file_mode & stat.S_IRUSR))
self.assertTrue(bool(file_mode & stat.S_IWUSR))
# only S_IRUSR and S_IWUSR are supported on Windows: https://docs.python.org/3.8/library/os.html#os.chmod
if os.name != 'nt':
self.assertFalse(bool(file_mode & stat.S_IXUSR))
self.assertFalse(bool(file_mode & stat.S_IRGRP))
self.assertFalse(bool(file_mode & stat.S_IWGRP))
self.assertFalse(bool(file_mode & stat.S_IXGRP))
self.assertFalse(bool(file_mode & stat.S_IROTH))
self.assertFalse(bool(file_mode & stat.S_IWOTH))
self.assertFalse(bool(file_mode & stat.S_IXOTH))
示例6: fsync
# 需要导入模块: import os [as 别名]
# 或者: from os import html [as 别名]
def fsync(self):
if self._oplog:
# https://docs.python.org/2/library/os.html#os.fsync
self._oplog.flush()
self._last_flush_time = time()
self._writes_unflushed = 0
return os.fsync(self._oplog.fileno())
示例7: read
# 需要导入模块: import os [as 别名]
# 或者: from os import html [as 别名]
def read(self, *args, **kwargs):
_size = None
try:
_size = kwargs.get('size', args[0])
except IndexError:
pass
return self.getvalue()[:_size]
# https://docs.python.org/2/library/stdtypes.html#bltin-file-objects Says file.write(str)
示例8: __import__
# 需要导入模块: import os [as 别名]
# 或者: from os import html [as 别名]
def __import__(*args, **kwargs):
global _is_debug
if _is_debug:
print "*** HOOKED *** IMPORT: args = %s, kwargs = %s" % (args, kwargs)
name = args[0]
try:
return _real_import(*args, **kwargs)
except ImportError:
# TODO: Add support for more extensions? (e.g. *.pyc)
if _mem_storage.has_key(args[0] + '.py'):
name = args[0] + '.py'
if _mem_storage.has_key(name):
# It's a Virtual File!
new_mod = imp.new_module(name)
exec _mem_storage[name].read() in new_mod.__dict__
sys.modules[args[0]] = new_mod
return new_mod
else:
# It's a bust!
raise ImportError('ImportError: No module named %s' % name)
# TODO: Monkey patch https://docs.python.org/2/library/os.html#file-descriptor-operations
示例9: safe_sleep
# 需要导入模块: import os [as 别名]
# 或者: from os import html [as 别名]
def safe_sleep(seconds):
"""Ensure that the thread sleeps at a minimum the requested seconds.
Until Python 3.5, there was no guarantee that time.sleep() would actually sleep the requested
time. See https://docs.python.org/3/library/time.html#time.sleep."""
if sys.version_info[0:2] >= (3, 5):
time.sleep(seconds)
else:
start_time = current_time = time.time()
while current_time - start_time < seconds:
remaining_time = seconds - (current_time - start_time)
time.sleep(remaining_time)
current_time = time.time()
示例10: finalize
# 需要导入模块: import os [as 别名]
# 或者: from os import html [as 别名]
def finalize(self, source=None):
"""Rename `work_dir` to `target_dir` using `os.rename()`.
:param str source: An optional source offset into the `work_dir`` to use for the atomic
update of `target_dir`. By default the whole `work_dir` is used.
If a race is lost and `target_dir` already exists, the `target_dir` dir is left unchanged and
the `work_dir` directory will simply be removed.
"""
if self.is_finalized:
return
source = os.path.join(self._work_dir, source) if source else self._work_dir
try:
# Perform an atomic rename.
#
# Per the docs: https://docs.python.org/2.7/library/os.html#os.rename
#
# The operation may fail on some Unix flavors if src and dst are on different filesystems.
# If successful, the renaming will be an atomic operation (this is a POSIX requirement).
#
# We have satisfied the single filesystem constraint by arranging the `work_dir` to be a
# sibling of the `target_dir`.
os.rename(source, self._target_dir)
except OSError as e:
if e.errno not in (errno.EEXIST, errno.ENOTEMPTY):
raise e
finally:
self.cleanup()
示例11: generate_256bit_key
# 需要导入模块: import os [as 别名]
# 或者: from os import html [as 别名]
def generate_256bit_key():
"""Generate a pseudo-random secure ready-for-crypto-use key.
Generate it straight using urandom. Proving randomness is impossible, and a good source
is a hotly debated subject. As always, opinions are welcome but please inform
yourself first and be prepared to cite a source.
Further Reading:
https://docs.python.org/3.5/library/os.html#os.urandom
https://docs.python.org/2.7/library/os.html#os.urandom
https://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers/
http://www.2uo.de/myths-about-urandom/
https://github.com/dlitz/pycrypto/blob/master/lib/Crypto/Random/__init__.py
"""
return binascii.hexlify(os.urandom(32))
示例12: contain
# 需要导入模块: import os [as 别名]
# 或者: from os import html [as 别名]
def contain(command):
# TODO: exec command, note the difference between the exec flavours
# https://docs.python.org/2/library/os.html#os.execv
# NOTE: command is an array (the first element is path/file, and the entire
# array is exec's args)
os._exit(0) # TODO: remove this after adding exec
示例13: allocate_shm_file
# 需要导入模块: import os [as 别名]
# 或者: from os import html [as 别名]
def allocate_shm_file(filename, nbytes, dbytes, readonly):
exists = os.path.exists(filename)
size = 0 if not exists else os.path.getsize(filename)
if readonly and not exists:
raise SharedMemoryReadError(filename + " has not been allocated. Requested " + str(nbytes) + " bytes.")
elif readonly and size != nbytes:
raise SharedMemoryReadError("{} exists, but the allocation size ({} bytes) does not match the request ({} bytes).".format(
filename, size, nbytes
))
if exists:
if size > nbytes:
with open(filename, 'wb') as f:
os.ftruncate(f.fileno(), nbytes)
elif size < nbytes:
# too small? just remake it below
os.unlink(filename)
exists = os.path.exists(filename)
if not exists:
# Previously we were writing out real files full of zeros,
# but a) that takes forever and b) modern OSes support sparse
# files (i.e. gigabytes of zeros that take up only a few real bytes).
#
# The following should take advantage of this functionality and be faster.
# It should work on Python 2.7 Unix, and Python 3.5+ on Unix and Windows.
#
# References:
# https://stackoverflow.com/questions/8816059/create-file-of-particular-size-in-python
# https://docs.python.org/3/library/os.html#os.ftruncate
# https://docs.python.org/2/library/os.html#os.ftruncate
#
with open(filename, 'wb') as f:
os.ftruncate(f.fileno(), nbytes)
示例14: _exit_processes
# 需要导入模块: import os [as 别名]
# 或者: from os import html [as 别名]
def _exit_processes(exit_code): # type: (int) -> None
"""Exit main thread and child processes.
For more information:
https://docs.python.org/2/library/os.html#process-management
https://docs.python.org/3/library/os.html#process-management
Args:
exit_code (int): exit code
"""
os._exit(exit_code) # pylint: disable=protected-access
示例15: test_create_folder
# 需要导入模块: import os [as 别名]
# 或者: from os import html [as 别名]
def test_create_folder(self):
folder_path = 'new_folder'
shared_utils.create_folder(folder_path)
self.assertTrue(os.path.isdir(folder_path))
# change just-created folder to be unaccessible
os.chmod(folder_path, 0o0)
# try creating a folder inside the unaccessible one
# NOTE: apparently there's no way to make a Windows folder
# read-only programmatically, so first check if the parent
# folder cannot be accessed. See Windows note in os.chmod()
# https://docs.python.org/3/library/os.html#os.chmod
blocked_folder_path = os.path.join(folder_path, 'blocked_folder')
if not os.access(folder_path, os.W_OK):
with self.assertRaises(OSError) as cm:
shared_utils.create_folder(blocked_folder_path)
self.assertEqual(cm.exception.errno, 13)
# try creating a folder with the same name as an existing file
file_path = 'new_file'
with open(file_path, "w+"):
with self.assertRaises(OSError) as cm:
shared_utils.create_folder(file_path)
self.assertEqual(cm.exception.errno, 17)
# remove artifacts
os.chmod(folder_path, 0o755)
if os.path.exists(folder_path):
os.rmdir(folder_path)
if os.path.exists(file_path):
os.remove(file_path)