Python os.rmdir() Examples
The following are 30
code examples of os.rmdir().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
os
, or try the search function
.
Example #1
Source File: get_data.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 10 votes |
def get_cifar10(data_dir): if not os.path.isdir(data_dir): os.system("mkdir " + data_dir) cwd = os.path.abspath(os.getcwd()) os.chdir(data_dir) if (not os.path.exists('train.rec')) or \ (not os.path.exists('test.rec')) : import urllib, zipfile, glob dirname = os.getcwd() zippath = os.path.join(dirname, "cifar10.zip") urllib.urlretrieve("http://data.mxnet.io/mxnet/data/cifar10.zip", zippath) zf = zipfile.ZipFile(zippath, "r") zf.extractall() zf.close() os.remove(zippath) for f in glob.glob(os.path.join(dirname, "cifar", "*")): name = f.split(os.path.sep)[-1] os.rename(f, os.path.join(dirname, name)) os.rmdir(os.path.join(dirname, "cifar")) os.chdir(cwd) # data
Example #2
Source File: tester.py From workload-collocation-agent with Apache License 2.0 | 6 votes |
def _delete_cgroup(cgroup_path): paths = _get_cgroup_full_path(cgroup_path) try: os.rmdir(paths[CgroupType.CPU]) except FileNotFoundError: log.warning('cpu cgroup "{}" not found'.format(cgroup_path)) try: os.rmdir(paths[CgroupType.MEMORY]) except FileNotFoundError: log.warning('memory cgroup "{}" not found'.format(cgroup_path)) try: os.rmdir(paths[CgroupType.CPUSET]) except FileNotFoundError: log.warning('cpuset cgroup "{}" not found'.format(cgroup_path)) try: os.rmdir(paths[CgroupType.PERF_EVENT]) except FileNotFoundError: log.warning('perf_event cgroup "{}" not found'.format(cgroup_path))
Example #3
Source File: util.py From jbox with MIT License | 6 votes |
def rollback(self): if not self.dry_run: for f in list(self.files_written): if os.path.exists(f): os.remove(f) # dirs should all be empty now, except perhaps for # __pycache__ subdirs # reverse so that subdirs appear before their parents dirs = sorted(self.dirs_created, reverse=True) for d in dirs: flist = os.listdir(d) if flist: assert flist == ['__pycache__'] sd = os.path.join(d, flist[0]) os.rmdir(sd) os.rmdir(d) # should fail if non-empty self._init_record()
Example #4
Source File: test_path_utils.py From toolium with Apache License 2.0 | 6 votes |
def test_create_new_folder_parallel(): folder = os.path.join('output', str(uuid.uuid4())) def run_makedirs(folder, exceptions): try: makedirs_safe(folder) except Exception as exc: exceptions.put(exc) for _ in range(5): exceptions = queue.Queue() thread1 = threading.Thread(target=run_makedirs, args=(folder, exceptions)) thread2 = threading.Thread(target=run_makedirs, args=(folder, exceptions)) thread1.start() thread2.start() thread1.join() thread2.join() assert exceptions.qsize() == 0 assert os.path.isdir(folder) os.rmdir(folder)
Example #5
Source File: file.py From gnocchi with Apache License 2.0 | 6 votes |
def _delete_measures_files_for_metric(self, metric_id, files): for f in files: try: os.unlink(self._build_measure_path(metric_id, f)) except OSError as e: # Another process deleted it in the meantime, no prob' if e.errno != errno.ENOENT: raise try: os.rmdir(self._build_measure_path(metric_id)) except OSError as e: # ENOENT: ok, it has been removed at almost the same time # by another process # ENOTEMPTY: ok, someone pushed measure in the meantime, # we'll delete the measures and directory later # EEXIST: some systems use this instead of ENOTEMPTY if e.errno not in (errno.ENOENT, errno.ENOTEMPTY, errno.EEXIST): raise
Example #6
Source File: test_file_api.py From mlimages with MIT License | 6 votes |
def test_prepare(self): p = get_data_folder() api = FileAPI(p) folders = ["one", "two"] relative = "/".join(folders) + "/three.txt" api.prepare_dir(relative) rmdirs = [] for f in folders: p = p + "/" + f self.assertTrue(os.path.exists(p)) rmdirs.append(p) self.assertFalse(os.path.exists(api.to_abs(relative))) for f in rmdirs[::-1]: os.rmdir(f)
Example #7
Source File: imagenet.py From nsf with MIT License | 6 votes |
def _download(self, root): if os.path.isdir(os.path.join(root, 'train')): return # Downloaded already os.makedirs(root, exist_ok=True) zip_file = os.path.join(root, self.UNZIPPED_DIR_NAME + '.zip') print('Downloading {}...'.format(os.path.basename(zip_file))) download_file_from_google_drive(self.GOOGLE_DRIVE_FILE_ID, zip_file) print('Extracting {}...'.format(os.path.basename(zip_file))) with zipfile.ZipFile(zip_file, 'r') as fp: fp.extractall(root) os.remove(zip_file) os.rename(os.path.join(root, self.UNZIPPED_DIR_NAME, self.UNZIPPED_TRAIN_SUBDIR), os.path.join(root, 'train')) os.rename(os.path.join(root, self.UNZIPPED_DIR_NAME, self.UNZIPPED_VAL_SUBDIR), os.path.join(root, 'val')) os.rmdir(os.path.join(root, self.UNZIPPED_DIR_NAME))
Example #8
Source File: inotify_file_watcher_test.py From browserscope with Apache License 2.0 | 6 votes |
def test_subdirectory_deleted(self): """Tests that internal _directory_to_subdirs is updated on delete.""" path = self._create_directory('test') sub_path = self._create_directory('test/test2') self._watcher.start() self.assertEqual( set([sub_path]), self._watcher._directory_to_subdirs[path]) os.rmdir(sub_path) self.assertEqual( set([sub_path]), self._watcher._get_changed_paths()) self.assertEqual( set(), self._watcher._directory_to_subdirs[path]) os.rmdir(path) self.assertEqual( set([path]), self._watcher._get_changed_paths())
Example #9
Source File: test_sim_struct.py From simnibs with GNU General Public License v3.0 | 6 votes |
def test_prepare_find_m2m(self, sphere3_fn): l = sim_struct.LEADFIELD() l.pathfem = '' l.fnamehead = sphere3_fn path, n = os.path.split(sphere3_fn) dir_fn = os.path.join(path, 'm2m_'+n[:-4]) os.mkdir(dir_fn) l._prepare() assert os.path.abspath(l.subpath) == os.path.abspath(dir_fn) os.rmdir(dir_fn) l = sim_struct.LEADFIELD() l.pathfem = '' l.fnamehead = sphere3_fn path, n = os.path.split(sphere3_fn) dir_fn = os.path.join(path, 'm2m_'+n[:-4]) os.mkdir(dir_fn) l._prepare() assert os.path.abspath(l.subpath) == os.path.abspath(dir_fn) os.rmdir(dir_fn)
Example #10
Source File: util.py From recruit with Apache License 2.0 | 6 votes |
def rollback(self): if not self.dry_run: for f in list(self.files_written): if os.path.exists(f): os.remove(f) # dirs should all be empty now, except perhaps for # __pycache__ subdirs # reverse so that subdirs appear before their parents dirs = sorted(self.dirs_created, reverse=True) for d in dirs: flist = os.listdir(d) if flist: assert flist == ['__pycache__'] sd = os.path.join(d, flist[0]) os.rmdir(sd) os.rmdir(d) # should fail if non-empty self._init_record()
Example #11
Source File: log_test.py From tornado-zh with MIT License | 6 votes |
def test_log_file(self): tmpdir = tempfile.mkdtemp() try: self.options.log_file_prefix = tmpdir + '/test_log' enable_pretty_logging(options=self.options, logger=self.logger) self.assertEqual(1, len(self.logger.handlers)) self.logger.error('hello') self.logger.handlers[0].flush() filenames = glob.glob(tmpdir + '/test_log*') self.assertEqual(1, len(filenames)) with open(filenames[0]) as f: self.assertRegexpMatches(f.read(), r'^\[E [^]]*\] hello$') finally: for handler in self.logger.handlers: handler.flush() handler.close() for filename in glob.glob(tmpdir + '/test_log*'): os.unlink(filename) os.rmdir(tmpdir)
Example #12
Source File: log_test.py From tornado-zh with MIT License | 6 votes |
def test_log_file_with_timed_rotating(self): tmpdir = tempfile.mkdtemp() try: self.options.log_file_prefix = tmpdir + '/test_log' self.options.log_rotate_mode = 'time' enable_pretty_logging(options=self.options, logger=self.logger) self.logger.error('hello') self.logger.handlers[0].flush() filenames = glob.glob(tmpdir + '/test_log*') self.assertEqual(1, len(filenames)) with open(filenames[0]) as f: self.assertRegexpMatches( f.read(), r'^\[E [^]]*\] hello$') finally: for handler in self.logger.handlers: handler.flush() handler.close() for filename in glob.glob(tmpdir + '/test_log*'): os.unlink(filename) os.rmdir(tmpdir)
Example #13
Source File: log_test.py From tornado-zh with MIT License | 6 votes |
def test_log_file_with_timed_rotating(self): tmpdir = tempfile.mkdtemp() try: self.options.log_file_prefix = tmpdir + '/test_log' self.options.log_rotate_mode = 'time' enable_pretty_logging(options=self.options, logger=self.logger) self.logger.error('hello') self.logger.handlers[0].flush() filenames = glob.glob(tmpdir + '/test_log*') self.assertEqual(1, len(filenames)) with open(filenames[0]) as f: self.assertRegexpMatches( f.read(), r'^\[E [^]]*\] hello$') finally: for handler in self.logger.handlers: handler.flush() handler.close() for filename in glob.glob(tmpdir + '/test_log*'): os.unlink(filename) os.rmdir(tmpdir)
Example #14
Source File: local.py From py with MIT License | 6 votes |
def remove(self, rec=1, ignore_errors=False): """ remove a file or directory (or a directory tree if rec=1). if ignore_errors is True, errors while removing directories will be ignored. """ if self.check(dir=1, link=0): if rec: # force remove of readonly files on windows if iswin32: self.chmod(0o700, rec=1) import shutil py.error.checked_call( shutil.rmtree, self.strpath, ignore_errors=ignore_errors) else: py.error.checked_call(os.rmdir, self.strpath) else: if iswin32: self.chmod(0o700) py.error.checked_call(os.remove, self.strpath)
Example #15
Source File: integration_tests.py From ProMo with MIT License | 6 votes |
def tearDown(self): '''Remove any files generated during the test''' #unittest.TestCase.tearDown(self) root = os.path.join(".", "files") endingList = os.listdir(root) endingDir = os.getcwd() rmList = [fn for fn in endingList if fn not in self.startingList] if self.oldRoot == root: for fn in rmList: fnFullPath = os.path.join(root, fn) if os.path.isdir(fnFullPath): os.rmdir(fnFullPath) else: os.remove(fnFullPath) os.chdir(self.oldRoot)
Example #16
Source File: test_utils.py From pysat with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_initial_pysat_load(self): import shutil saved = False try: root = os.path.join(os.getenv('HOME'), '.pysat') new_root = os.path.join(os.getenv('HOME'), '.saved_pysat') shutil.move(root, new_root) saved = True except: pass re_load(pysat) try: if saved: # remove directory, trying to be careful os.remove(os.path.join(root, 'data_path.txt')) os.rmdir(root) shutil.move(new_root, root) except: pass assert True
Example #17
Source File: deploy-dashboard-vm.py From JetPack with Apache License 2.0 | 6 votes |
def create_floppy_image(vlock_filename, floppy_image): """ Creates the floppy image used to install the vlock file """ # Delete any existing image to start clean if os.access(floppy_image, os.R_OK): os.unlink(floppy_image) subprocess.check_call("mkfs.vfat -C {} 1440".format(floppy_image), shell=True) floppy_mnt = "/tmp/mnt-dashboard" os.mkdir(floppy_mnt) subprocess.check_call("mount -o loop {} {}".format(floppy_image, floppy_mnt), shell=True) shutil.copy(vlock_filename, os.path.join(floppy_mnt, "versionlock.list")) subprocess.check_call("umount {}".format(floppy_mnt), shell=True) os.rmdir(floppy_mnt)
Example #18
Source File: dtest_setup.py From cassandra-dtest with Apache License 2.0 | 5 votes |
def cleanup_cluster(self): with log_filter('cassandra'): # quiet noise from driver when nodes start going down if self.dtest_config.keep_test_dir: self.cluster.stop(gently=self.dtest_config.enable_jacoco_code_coverage) else: # when recording coverage the jvm has to exit normally # or the coverage information is not written by the jacoco agent # otherwise we can just kill the process if self.dtest_config.enable_jacoco_code_coverage: self.cluster.stop(gently=True) # Cleanup everything: try: if self.log_watch_thread: self.stop_active_log_watch() finally: logger.debug("removing ccm cluster {name} at: {path}".format(name=self.cluster.name, path=self.test_path)) self.cluster.remove() logger.debug("clearing ssl stores from [{0}] directory".format(self.test_path)) for filename in ('keystore.jks', 'truststore.jks', 'ccm_node.cer'): try: os.remove(os.path.join(self.test_path, filename)) except OSError as e: # ENOENT = no such file or directory assert e.errno == errno.ENOENT os.rmdir(self.test_path) self.cleanup_last_test_dir()
Example #19
Source File: resctrl.py From workload-collocation-agent with Apache License 2.0 | 5 votes |
def remove(self, mongroup_name): """Remove resctrl control directory or just mon_group if this is root or not last container under control. """ # Try to clean itself if I'm the last mon_group and not root. if self.name == RESCTRL_ROOT_NAME: log.debug('resctrl: remove root') dir_to_remove = self._get_mongroup_fullpath(mongroup_name) else: # For non root # Am I last on the remove all. if len(self.get_mon_groups()) == 1: log.debug('resctrl: remove ctrl directory %r', self.name) dir_to_remove = self.fullpath else: log.debug('resctrl: remove just mon_group %r in %r', mongroup_name, self.name) dir_to_remove = self._get_mongroup_fullpath(mongroup_name) # Remove the mongroup directory if exists. with SetEffectiveRootUid(): log.log(logger.TRACE, 'resctrl: rmdir(%r)', dir_to_remove) if os.path.exists(dir_to_remove): os.rmdir(dir_to_remove) else: log.debug("Directory: {} already removed. Ignoring!".format(dir_to_remove))
Example #20
Source File: resctrl.py From workload-collocation-agent with Apache License 2.0 | 5 votes |
def clean_taskless_groups(mon_groups_relation: Dict[str, List[str]]): """Remove all control and monitoring group based on list of already read groups from mon_groups_relation. """ for ctrl_group, mon_groups in mon_groups_relation.items(): for mon_group in mon_groups: ctrl_group_dir = os.path.join(BASE_RESCTRL_PATH, ctrl_group) mon_group_dir = os.path.join(ctrl_group_dir, MON_GROUPS, mon_group) tasks_filename = os.path.join(mon_group_dir, TASKS_FILENAME) mon_groups_to_remove = [] with open(tasks_filename) as tasks_file: if tasks_file.read() == '': mon_groups_to_remove.append(mon_group_dir) if mon_groups_to_remove: log.debug('mon_groups_to_remove: %r', mon_groups_to_remove) # For ech non root group, drop just ctrl group if all mon groups are empty if ctrl_group != '' and \ len(mon_groups_to_remove) == len(mon_groups_relation[ctrl_group]): log.log(TRACE, 'rmdir(%r)', ctrl_group_dir) os.rmdir(ctrl_group_dir) else: for mon_group_to_remove in mon_groups_to_remove: os.rmdir(mon_group_to_remove) log.log(TRACE, 'rmdir(%r)', mon_group_to_remove)
Example #21
Source File: cache_manager.py From openSUSE-release-tools with GNU General Public License v2.0 | 5 votes |
def prune_all(): if CacheManager.pruned: return CacheManager.pruned = True prune_lock = os.path.join(CacheManager.directory(), '.prune') if not os.path.exists(prune_lock): CacheManager.migrate() elif time() - os.stat(prune_lock).st_mtime < CacheManager.PRUNE_FREQUENCY: return with open(prune_lock, 'a'): os.utime(prune_lock, None) print('> pruning cache', file=sys.stderr) accessed_prune = time() - CacheManager.PRUNE_TTL files_pruned = 0 bytes_pruned = 0 for directory, subdirectories, files in os.walk(CacheManager.directory()): files_pruned_directory = 0 for filename in files: path = os.path.join(directory, filename) stat = os.stat(path) accessed = stat.st_atime if accessed < accessed_prune: files_pruned_directory += 1 files_pruned += 1 bytes_pruned += stat.st_size os.remove(path) if len(subdirectories) == 0 and len(files) - files_pruned_directory == 0: os.rmdir(directory) print('> pruned {:,} files comprised of {:,} bytes'.format( files_pruned, bytes_pruned), file=sys.stderr) # Migrate the variety of prior cache locations within a single parent.
Example #22
Source File: conftest.py From ideascube with GNU Affero General Public License v3.0 | 5 votes |
def backup_root(request): os.makedirs(BACKUPS_ROOT, exist_ok=True) def fin(): os.rmdir(BACKUPS_ROOT) request.addfinalizer(fin)
Example #23
Source File: cache_manager.py From openSUSE-release-tools with GNU General Public License v2.0 | 5 votes |
def prune_all(): if CacheManager.pruned: return CacheManager.pruned = True prune_lock = os.path.join(CacheManager.directory(), '.prune') if not os.path.exists(prune_lock): CacheManager.migrate() elif time() - os.stat(prune_lock).st_mtime < CacheManager.PRUNE_FREQUENCY: return with open(prune_lock, 'a'): os.utime(prune_lock, None) print('> pruning cache', file=sys.stderr) accessed_prune = time() - CacheManager.PRUNE_TTL files_pruned = 0 bytes_pruned = 0 for directory, subdirectories, files in os.walk(CacheManager.directory()): files_pruned_directory = 0 for filename in files: path = os.path.join(directory, filename) stat = os.stat(path) accessed = stat.st_atime if accessed < accessed_prune: files_pruned_directory += 1 files_pruned += 1 bytes_pruned += stat.st_size os.remove(path) if len(subdirectories) == 0 and len(files) - files_pruned_directory == 0: os.rmdir(directory) print('> pruned {:,} files comprised of {:,} bytes'.format( files_pruned, bytes_pruned), file=sys.stderr) # Migrate the variety of prior cache locations within a single parent.
Example #24
Source File: file_util.py From OP_Manager with MIT License | 5 votes |
def deleteHelper(srclist): """ Takes in a list of full walk path(str) for deletion. And render the delete on the display while deleting. For Example: [filePath1, filePath2, filePath3.......filePath n] or [DirPath1, DirPath2, DirPath3,...., DirPath n] :param srclist: list of paths to file or directory :return: NA """ GPIO_Init.displayPng(workDir + "/Assets/Img/Deleting.png") time.sleep(0.5) for f in srclist: if isdir(f): # If given element in a list is a directory shutil.rmtree(srclist[0]) else: folder = dirname(f) if os.path.exists(f): # Check for file existence os.remove(f) if len(os.listdir(folder)) == 0: # If nothing is in the folder, remove the parent folder os.rmdir(folder) GPIO_Init.displayPng(workDir + "/Assets/Img/Done.png") GPIO_Init.getAnyKeyEvent() # Press any key to proceed return
Example #25
Source File: test_databases.py From workload-collocation-agent with Apache License 2.0 | 5 votes |
def test_localdatabase_enable_to_reuse_db(): LocalDatabase('/tmp/test_wca_db').set(b'key', b'value') assert LocalDatabase('/tmp/test_wca_db').get(b'key') == b'value' os.remove('/tmp/test_wca_db/key') os.rmdir('/tmp/test_wca_db')
Example #26
Source File: rearrange_files.py From coursys with GNU General Public License v3.0 | 5 votes |
def move_file(model, storage, field_name, field, o): fld = getattr(o, field_name) old_loc = fld.name if not old_loc: # null in optional field: nothing to do. return new_loc = field.upload_to(o, os.path.split(old_loc)[1]) old_loc_full = storage.path(old_loc) new_loc_full = storage.path(new_loc) old_dir = os.path.split(old_loc_full)[0] new_dir = os.path.split(new_loc_full)[0] if old_loc == new_loc or uuid_re.search(old_loc): # already in place or contains a UUID (i.e. is probably new format already) return # link/move/unlink ensures that this can be safely interrupted print(('%s > %s' % (old_loc, new_loc))) os.makedirs(new_dir) os.link(old_loc_full, new_loc_full) up = {field_name: new_loc} model.objects.filter(pk=o.pk).update(**up) # avoid any .save() logic os.remove(old_loc_full) # clean up newly-empty directories while not os.listdir(old_dir): os.rmdir(old_dir) old_dir = os.path.split(old_dir)[0]
Example #27
Source File: test_fuse.py From filesystem_spec with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_basic(tmpdir): mountdir = str(tmpdir.mkdir("mount")) fuse_process = Process(target=host_fuse, args=(str(mountdir),)) fuse_process.start() try: timeout = 10 while True: try: # can fail with device not ready while waiting for fuse if "testfile" in os.listdir(mountdir): break except Exception: pass timeout -= 1 time.sleep(1) assert timeout > 0, "Timeout" fn = os.path.join(mountdir, "test") with open(fn, "wb") as f: f.write(b"data") with open(fn) as f: assert f.read() == "data" os.remove(fn) os.mkdir(fn) assert os.listdir(fn) == [] os.mkdir(fn + "/inner") with pytest.raises(OSError): os.rmdir(fn) os.rmdir(fn + "/inner") os.rmdir(fn) finally: os.kill(fuse_process.pid, signal.SIGTERM) fuse_process.join()
Example #28
Source File: droidutil.py From droidlysis with MIT License | 5 votes |
def on_rm_tree_error(fn, path, exc_info): """ Error handler for ``shutil.rmtree``. rmtree fails in particular if the file to delete is read-only. to remove, we attempt to set all permissions and then retry. Usage : ``shutil.rmtree(path, onerror=onerror)`` """ if fn is os.rmdir: os.chmod(path, 777) os.rmdir(path) elif fn is os.remove: os.chmod(path, 777) os.remove(path)
Example #29
Source File: local.py From filesystem_spec with BSD 3-Clause "New" or "Revised" License | 5 votes |
def rmdir(self, path): path = self._strip_protocol(path) os.rmdir(path)
Example #30
Source File: mkdirlockfile.py From recruit with Apache License 2.0 | 5 votes |
def break_lock(self): if os.path.exists(self.lock_file): for name in os.listdir(self.lock_file): os.unlink(os.path.join(self.lock_file, name)) os.rmdir(self.lock_file)