本文整理汇总了Python中os.removedirs方法的典型用法代码示例。如果您正苦于以下问题:Python os.removedirs方法的具体用法?Python os.removedirs怎么用?Python os.removedirs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.removedirs方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mkdir_permissions
# 需要导入模块: import os [as 别名]
# 或者: from os import removedirs [as 别名]
def mkdir_permissions(request):
""" Fixture for creating dir with specific read/write permissions """
def make_mkdir(read=False, write=False):
if read and write:
mode = 0o755
elif read and not write:
mode = 0o555
elif not read and write:
mode = 0o333
elif not read and not write:
mode = 0o000
path = mkdtemp()
os.chmod(path, mode)
def fin():
os.chmod(path, 0o755)
os.removedirs(path)
request.addfinalizer(fin)
return path
return make_mkdir
示例2: test_test_cache
# 需要导入模块: import os [as 别名]
# 或者: from os import removedirs [as 别名]
def test_test_cache(mkdir_permissions):
# Test when cache dir exists already
path = mkdir_permissions(read=False, write=False)
assert (False, False) == cache.test_cache(dict(cache_line_dir=path))
path = mkdir_permissions(read=False, write=True)
assert (False, True) == cache.test_cache(dict(cache_line_dir=path))
path = mkdir_permissions(read=True, write=False)
assert (True, False) == cache.test_cache(dict(cache_line_dir=path))
path = mkdir_permissions(read=True, write=True)
assert (True, True) == cache.test_cache(dict(cache_line_dir=path))
# Test when cache dir doesn't exist
tmp = os.path.join(tempfile.tempdir,
next(tempfile._get_candidate_names()) + '_yatsm')
read_write = cache.test_cache(dict(cache_line_dir=tmp))
os.removedirs(tmp)
assert (True, True) == read_write
示例3: test_parseable_file_path
# 需要导入模块: import os [as 别名]
# 或者: from os import removedirs [as 别名]
def test_parseable_file_path(self):
file_name = "test_target.py"
fake_path = HERE + os.getcwd()
module = join(fake_path, file_name)
try:
# create module under directories which have the same name as reporter.path_strip_prefix
# e.g. /src/some/path/src/test_target.py when reporter.path_strip_prefix = /src/
os.makedirs(fake_path)
with open(module, "w") as test_target:
test_target.write("a,b = object()")
self._test_output(
[module, "--output-format=parseable"],
expected_output=join(os.getcwd(), file_name),
)
finally:
os.remove(module)
os.removedirs(fake_path)
示例4: _init_dirs
# 需要导入模块: import os [as 别名]
# 或者: from os import removedirs [as 别名]
def _init_dirs(self):
test_dirs = ["a a", "b", "D_"]
config = "improbable"
root = tempfile.mkdtemp()
def cleanup():
try:
os.removedirs(root)
except (FileNotFoundError, OSError):
pass
os.chdir(root)
for dir_ in test_dirs:
os.mkdir(dir_, 0o0750)
f = "{0}.toml".format(config)
flags = os.O_WRONLY | os.O_CREAT
rel_path = "{0}/{1}".format(dir_, f)
abs_file_path = os.path.join(root, rel_path)
with os.fdopen(os.open(abs_file_path, flags, 0o0640), "w") as fp:
fp.write('key = "value is {0}"\n'.format(dir_))
return root, config, cleanup
示例5: sorter
# 需要导入模块: import os [as 别名]
# 或者: from os import removedirs [as 别名]
def sorter(user_directory, api_type, location, metadata):
legacy_directory = os.path.join(user_directory, api_type, location)
if not os.path.isdir(legacy_directory):
return
legacy_files = os.listdir(legacy_directory)
metadata_directory = os.path.join(
user_directory, "Metadata", api_type+".json")
results = list(chain(*metadata["valid"]))
for result in results:
legacy_filepath = os.path.join(legacy_directory, result["filename"])
filepath = os.path.join(result["directory"], result["filename"])
if result["filename"] in legacy_files:
if os.path.isfile(filepath):
same_file = filecmp.cmp(
legacy_filepath, filepath, shallow=False)
if same_file:
os.remove(filepath)
else:
os.remove(legacy_filepath)
continue
shutil.move(legacy_filepath, filepath)
if not os.listdir(legacy_directory):
os.removedirs(legacy_directory)
示例6: renames
# 需要导入模块: import os [as 别名]
# 或者: from os import removedirs [as 别名]
def renames(old, new):
# type: (str, str) -> None
"""Like os.renames(), but handles renaming across devices."""
# Implementation borrowed from os.renames().
head, tail = os.path.split(new)
if head and tail and not os.path.exists(head):
os.makedirs(head)
shutil.move(old, new)
head, tail = os.path.split(old)
if head and tail:
try:
os.removedirs(head)
except OSError:
pass
示例7: delete_output_files_jobs
# 需要导入模块: import os [as 别名]
# 或者: from os import removedirs [as 别名]
def delete_output_files_jobs(self, recursive=False):
"""
Delete the output files of all finished jobs in the current project and in all subprojects if recursive=True is
selected.
Args:
recursive (bool): [True/False] delete the output files of all jobs in all subprojects - default=False
"""
for job_id in self.get_job_ids(recursive=recursive):
job = self.inspect(job_id)
if job.status == "finished":
for file in job.list_files():
fullname = os.path.join(job.working_directory, file)
if os.path.isfile(fullname) and ".h5" not in fullname:
os.remove(fullname)
elif os.path.isdir(fullname):
os.removedirs(fullname)
示例8: RemoveAllStalePycFiles
# 需要导入模块: import os [as 别名]
# 或者: from os import removedirs [as 别名]
def RemoveAllStalePycFiles(base_dir):
"""Scan directories for old .pyc files without a .py file and delete them."""
for dirname, _, filenames in os.walk(base_dir):
if '.git' in dirname:
continue
for filename in filenames:
root, ext = os.path.splitext(filename)
if ext != '.pyc':
continue
pyc_path = os.path.join(dirname, filename)
py_path = os.path.join(dirname, root + '.py')
try:
if not os.path.exists(py_path):
os.remove(pyc_path)
except OSError:
# Wrap OS calls in try/except in case another process touched this file.
pass
try:
os.removedirs(dirname)
except OSError:
# Wrap OS calls in try/except in case another process touched this dir.
pass
示例9: do_clear_data_by_user
# 需要导入模块: import os [as 别名]
# 或者: from os import removedirs [as 别名]
def do_clear_data_by_user(QQ, conn):
DATA_DIR_KEY = BASE_DIR + QQ + '/'
WEB_IMAGE_PATH_DIR = WEB_IMAGE_PATH + QQ + '/'
if os.path.exists(DATA_DIR_KEY):
# 删除有QQ号的所有key
# 该方法在docker中无法使用,因为该容器内无redis-cli
# delete_cmd = "redis-cli KEYS \"*" + QQ + "*\"|xargs redis-cli DEL"
# print(delete_cmd)
# os.system(delete_cmd)
# 删除 该路径下所有文件
os.system("rm -rf " + DATA_DIR_KEY)
os.system("rm -rf " + WEB_IMAGE_PATH_DIR)
conn.hdel(USER_MAP_KEY, QQ)
conn.lrem(WAITING_USER_LIST, 0, QQ)
# redis的del不支持正则表达式,因此只能循环删除
all_keys = conn.keys("*" + QQ + "*")
print()
for key in all_keys:
conn.delete(key)
# os.removedirs(os.path.join(BASE_DIR, QQ))
finish = 1
else:
finish = 2
return finish
示例10: remove_dir
# 需要导入模块: import os [as 别名]
# 或者: from os import removedirs [as 别名]
def remove_dir(dir_path):
print("删除文件夹的目录是:"+dir_path)
# 如果是空文件夹
if not os.listdir(dir_path):
os.removedirs(dir_path)
for root, dirs, files in os.walk(dir_path, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
示例11: renames
# 需要导入模块: import os [as 别名]
# 或者: from os import removedirs [as 别名]
def renames(old, new):
"""Like os.renames(), but handles renaming across devices."""
# Implementation borrowed from os.renames().
head, tail = os.path.split(new)
if head and tail and not os.path.exists(head):
os.makedirs(head)
shutil.move(old, new)
head, tail = os.path.split(old)
if head and tail:
try:
os.removedirs(head)
except OSError:
pass
示例12: clean_path
# 需要导入模块: import os [as 别名]
# 或者: from os import removedirs [as 别名]
def clean_path(path):
if not path:
logging.info("Directory clean up - empty dir passed")
return
logging.info("Directory clean up - removing %s", path)
try:
# TODO: need to use osfs-rmdir on VSAN. For now jus yell if it failed
os.removedirs(path)
except Exception as e:
logging.warning("Directory clean up failed - %s, err: %s", path, e)
示例13: tearDown
# 需要导入模块: import os [as 别名]
# 或者: from os import removedirs [as 别名]
def tearDown(self):
path = os.path.join(test_support.TESTFN, 'dir1', 'dir2', 'dir3',
'dir4', 'dir5', 'dir6')
# If the tests failed, the bottom-most directory ('../dir6')
# may not have been created, so we look for the outermost directory
# that exists.
while not os.path.exists(path) and path != test_support.TESTFN:
path = os.path.dirname(path)
os.removedirs(path)
示例14: removedirs
# 需要导入模块: import os [as 别名]
# 或者: from os import removedirs [as 别名]
def removedirs(self):
os.removedirs(self)
# --- Modifying operations on files
示例15: _delete_file
# 需要导入模块: import os [as 别名]
# 或者: from os import removedirs [as 别名]
def _delete_file(configurator, path):
""" remove file and remove it's directories if empty """
path = os.path.join(configurator.target_directory, path)
os.remove(path)
try:
os.removedirs(os.path.dirname(path))
except OSError:
pass