當前位置: 首頁>>代碼示例>>Python>>正文


Python shutil.rmtree方法代碼示例

本文整理匯總了Python中shutil.rmtree方法的典型用法代碼示例。如果您正苦於以下問題:Python shutil.rmtree方法的具體用法?Python shutil.rmtree怎麽用?Python shutil.rmtree使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在shutil的用法示例。


在下文中一共展示了shutil.rmtree方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __call__

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import rmtree [as 別名]
def __call__(self, args, env):
        samples_dir = env.get('samples-dir')
        for label in ("cat", "dog"):
            dest = os.path.join(samples_dir, label)
            if os.path.exists(dest):
                raise VergeMLError("Directory {} already exists in samples dir: {}".format(label, dest))
        print("Downloading cats and dogs to {}.".format(samples_dir))
        src_dir = self.download_files([(_URL, "catsdogs.zip")], env)
        path = os.path.join(src_dir, "catsdogs.zip")

        print("Extracting data.")
        zipf = zipfile.ZipFile(path, 'r')
        zipf.extractall(src_dir)
        zipf.close()

        for file, dest in (("PetImages/Dog", "dog"), ("PetImages/Cat", "cat")):
            shutil.copytree(os.path.join(src_dir, file), os.path.join(samples_dir, dest))

        shutil.rmtree(src_dir)

        # WTF?
        os.unlink(os.path.join(samples_dir, "cat", "666.jpg"))
        os.unlink(os.path.join(samples_dir, "dog", "11702.jpg"))

        print("Finished downloading cats and dogs.") 
開發者ID:mme,項目名稱:vergeml,代碼行數:27,代碼來源:cats_and_dogs.py

示例2: _download_straight_dope_notebooks

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import rmtree [as 別名]
def _download_straight_dope_notebooks():
    """Downloads the Straight Dope Notebooks.

    Returns:
        True if it succeeds in downloading the notebooks without error.
    """
    logging.info('Cleaning and setting up notebooks directory "{}"'.format(NOTEBOOKS_DIR))
    shutil.rmtree(NOTEBOOKS_DIR, ignore_errors=True)

    cmd = [GIT_PATH,
           'clone',
           GIT_REPO,
           NOTEBOOKS_DIR]

    proc, msg = _run_command(cmd)

    if proc.returncode != 0:
        err_msg = 'Error downloading Straight Dope notebooks.\n'
        err_msg += msg
        logging.error(err_msg)
        return False
    return True 
開發者ID:awslabs,項目名稱:dynamic-training-with-apache-mxnet-on-aws,代碼行數:24,代碼來源:straight_dope_test_utils.py

示例3: test_multiprocessing_download_successful

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import rmtree [as 別名]
def test_multiprocessing_download_successful():
    """ test download with multiprocessing """
    tmp = tempfile.mkdtemp()
    tmpfile = os.path.join(tmp, 'README.md')
    process_list = []
    # test it with 10 processes
    for i in range(10):
        process_list.append(mp.Process(
            target=_download_successful, args=(tmpfile,)))
        process_list[i].start()
    for i in range(10):
        process_list[i].join()
    assert os.path.getsize(tmpfile) > 100, os.path.getsize(tmpfile)
    # check only one file we want left
    pattern = os.path.join(tmp, 'README.md*')
    assert len(glob.glob(pattern)) == 1, glob.glob(pattern)
    # delete temp dir
    shutil.rmtree(tmp) 
開發者ID:awslabs,項目名稱:dynamic-training-with-apache-mxnet-on-aws,代碼行數:20,代碼來源:test_gluon_utils.py

示例4: __call__

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import rmtree [as 別名]
def __call__(self, args, env):
        samples_dir = env.get('samples-dir')

        print("Downloading unique objects to {}.".format(samples_dir))

        src_dir = self.download_files([_URL], env=env, dir=env.get('cache-dir'))
        path = os.path.join(src_dir, "ObjectsAll.zip")

        zipf = zipfile.ZipFile(path, 'r')
        zipf.extractall(src_dir)
        zipf.close()

        for file in os.listdir(os.path.join(src_dir, "OBJECTSALL")):
            shutil.copy(os.path.join(src_dir, "OBJECTSALL", file), samples_dir)

        shutil.rmtree(src_dir)

        print("Finished downloading unique objects.") 
開發者ID:mme,項目名稱:vergeml,代碼行數:20,代碼來源:unique_objects.py

示例5: do_iteration

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import rmtree [as 別名]
def do_iteration(self, mutation_fn, aggression):
        """
        This method is called with an output mutation filename and a
        real aggression (see :ref:`aggression`) indicating the amount
        of aggression the fuzzing algorithm should use.  *mutation_fn*
        is unique for every invokation of :meth:`do_iteration`.

        It is an error for this method not to write the mutated
        template to *mutation_fn* before returning a result.  If a
        result is not found, the mutation filename may be written to
        if needed, but it is not required.

        If a notable result is found, it should be returned as a
        :class:`FuzzResult` instance.  This will be stored and reported
        to the ALF central server at the next check-in interval.  A
        return of None indicates a result was not found.

        The filenames of any temporary files or folders created during
        execution can be safely removed using :func:`alf.delete`.  This
        is safer than using :func:`os.remove` or :func:`shutil.rmtree`
        directly.  *mutation_fn* does not need to be deleted, it is
        cleaned up automatically.
        """
        raise NotImplementedError() 
開發者ID:blackberry,項目名稱:ALF,代碼行數:26,代碼來源:__init__.py

示例6: _delete_directory_contents

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import rmtree [as 別名]
def _delete_directory_contents(self, dirpath, filter_func):
        """Delete all files in a directory.

        :param dirpath: path to directory to clear
        :type dirpath: ``unicode`` or ``str``
        :param filter_func function to determine whether a file shall be
            deleted or not.
        :type filter_func ``callable``

        """
        if os.path.exists(dirpath):
            for filename in os.listdir(dirpath):
                if not filter_func(filename):
                    continue
                path = os.path.join(dirpath, filename)
                if os.path.isdir(path):
                    shutil.rmtree(path)
                else:
                    os.unlink(path)
                self.logger.debug('deleted : %r', path) 
開發者ID:TKkk-iOSer,項目名稱:wechat-alfred-workflow,代碼行數:22,代碼來源:workflow.py

示例7: test_extraction

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import rmtree [as 別名]
def test_extraction():
    import shutil

    src = 'c:/Users/shinj/Downloads/clonezilla-live-2.5.2-31-amd64.iso'
    tmp_dir = 'c:/Users/shinj/Documents/tmp'
    for subdir, pattern in [
            ('single_string', 'EFI/'),
            ('single_list', ['EFI/']),
            ('multi', ['EFI/', 'syslinux/']),
            ('all', None) ]:
        dest_dir = os.path.join(tmp_dir, subdir)
        if os.path.exists(dest_dir):
            shutil.rmtree(dest_dir)
        os.mkdir(dest_dir)
        args = [src, dest_dir]
        if pattern is not None:
            args.append(pattern)
        print ('Calling extract_iso(%s)' % args)
        extract_iso(*args) 
開發者ID:mbusb,項目名稱:multibootusb,代碼行數:21,代碼來源:_7zip.py

示例8: save_dictionary

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import rmtree [as 別名]
def save_dictionary(config):
    """
    :param config: config
    :return:
    """
    if config.save_dict is True:
        if os.path.exists(config.dict_directory):
            shutil.rmtree(config.dict_directory)
        if not os.path.isdir(config.dict_directory):
            os.makedirs(config.dict_directory)

        config.word_dict_path = "/".join([config.dict_directory, config.word_dict])
        config.label_dict_path = "/".join([config.dict_directory, config.label_dict])
        print("word_dict_path : {}".format(config.word_dict_path))
        print("label_dict_path : {}".format(config.label_dict_path))
        save_dict2file(config.create_alphabet.word_alphabet.words2id, config.word_dict_path)
        save_dict2file(config.create_alphabet.label_alphabet.words2id, config.label_dict_path)
        # copy to mulu
        print("copy dictionary to {}".format(config.save_dir))
        shutil.copytree(config.dict_directory, "/".join([config.save_dir, config.dict_directory]))


# load data / create alphabet / create iterator 
開發者ID:bamtercelboo,項目名稱:pytorch_NER_BiLSTM_CNN_CRF,代碼行數:25,代碼來源:mainHelp.py

示例9: test_creation

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import rmtree [as 別名]
def test_creation(self):
        if os.path.exists(self.base_dir):
            shutil.rmtree(self.checkpoints_dir, ignore_errors=True)

        try:
            FileStructManager(base_dir=self.base_dir, is_continue=False)
        except FileStructManager.FSMException as err:
            self.fail("Raise error when base directory exists: [{}]".format(err))

        self.assertFalse(os.path.exists(self.base_dir))

        try:
            FileStructManager(base_dir=self.base_dir, is_continue=False)
        except FileStructManager.FSMException as err:
            self.fail("Raise error when base directory exists but empty: [{}]".format(err))

        os.makedirs(os.path.join(self.base_dir, 'new_dir'))
        try:
            FileStructManager(base_dir=self.base_dir, is_continue=False)
        except:
            self.fail("Error initialize when exists non-registered folders in base directory")

        shutil.rmtree(self.base_dir, ignore_errors=True) 
開發者ID:toodef,項目名稱:neural-pipeline,代碼行數:25,代碼來源:utils_test.py

示例10: dotplot2

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import rmtree [as 別名]
def dotplot2(s1, s2, wordsize=5, overlap=5, verbose=1):
        """ verbose = 0 (no progress), 1 (progress if s1 and s2 are long) or
        2 (progress in any case) """
        doProgress = False
        if verbose > 1 or len(s1)*len(s2) > 1e6:
            doProgress = True

        mat = numpy.ones(((len(s1)-wordsize)/overlap+2, (len(s2)-wordsize)/overlap+2))

        for i in range(0, len(s1)-wordsize, overlap):
            if i % 1000 == 0 and doProgress:
                logging.info("  dotplot progress: {} of {} rows done".format(i, len(s1)-wordsize))
            word1 = s1[i:i+wordsize]

            for j in range(0, len(s2)-wordsize, overlap):
                word2 = s2[j:j+wordsize]

                if word1 == word2 or word1 == word2[::-1]:
                    mat[i/overlap, j/overlap] = 0
        
        imgData = None
        tempDir = tempfile.mkdtemp()
        try:
            path = os.path.join(tempDir, "dotplot.png")
            misc.imsave(path, mat)
            imgData = open(path).read()
        except Exception as e:
            logging.error("Error generating dotplots:'{}'".format(e))
        finally:
            shutil.rmtree(tempDir)
        return imgData 
開發者ID:svviz,項目名稱:svviz,代碼行數:33,代碼來源:dotplots.py

示例11: __del__

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import rmtree [as 別名]
def __del__(self):
        '''
            Called when the instance is about to be destroyed.
        '''
        if hasattr(self, '_tmpdir'):
            self._logger.info('Clean up temporary directory "{0}".'.format(self._tmpdir))
            shutil.rmtree(self._tmpdir) 
開發者ID:apache,項目名稱:incubator-spot,代碼行數:9,代碼來源:collector.py

示例12: fix_plugin_directories

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import rmtree [as 別名]
def fix_plugin_directories(dest):
    """
    Loops through all folders in the output directory, reads the their manifest
    file, and renames the directory to the standard <platform>_<guid> format
    """
    # Loop through directories in the destination directory
    for existing_dir in os.listdir(dest):
        existing_path = os.path.join(dest, existing_dir)

        # Skip non-directories
        if not os.path.isdir(existing_path):
            continue

        try:
            with open(os.path.join(existing_path, 'manifest.json')) as m:
                data = json.load(m)
                platform = data['platform']
                guid = data['guid']

                # Close json file
                m.close()

                expected_dir = platform + '_' + guid
                expected_path = os.path.join(dest, expected_dir)

                if existing_path != expected_path:
                    print('NOTICE: Folder should be "{}", but it is named "{}"'
                          .format(expected_dir, existing_dir))

                    if os.path.isdir(expected_path):
                        print('NOTICE: Correct pathed plugin already exists,'
                              + ' deleting extra plugin')
                        shutil.rmtree(existing_path)
                    else:
                        print('NOTICE: Renaming folder to proper name')
                        shutil.move(existing_path, expected_path)
        except (FileNotFoundError, json.decoder.JSONDecodeError, KeyError):
            print('ERROR: Could not read plugin data from {} folder'
                  .format(existing_path)) 
開發者ID:Slashbunny,項目名稱:gog-galaxy-plugin-downloader,代碼行數:41,代碼來源:download.py

示例13: delete_old_plugins

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import rmtree [as 別名]
def delete_old_plugins(data, dest):
    """
    Deletes versions of plugins that don't match the yaml manifest. In theory
    this should only be older versions, but any version that doesn't match
    the yaml definition will be deleted

    This explicitly does not touch other directories that do not match the
    known plugin names.

    If the version doesn't match the yaml definition, the directory is removed
    """
    # Loop over each plugin
    for name, data in data.items():
        expected_plugin_dir = name + '_' + data['guid']

        # Loop through directories in the destination directory
        for item in os.listdir(dest):
            full_path = os.path.join(dest, item)

            # Skip non-directories
            if not os.path.isdir(full_path):
                continue

            # Skip directory names that are in the valid plugin directory array
            if item == expected_plugin_dir:
                continue

            # If any other directory begins with <plugin_name>_, delete it
            if item.startswith(name + '_'):
                print('Deleting wrong version "{}" from "{}"'
                      .format(item, dest))
                shutil.rmtree(full_path) 
開發者ID:Slashbunny,項目名稱:gog-galaxy-plugin-downloader,代碼行數:34,代碼來源:download.py

示例14: download

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import rmtree [as 別名]
def download(env):
    """60000 tiny colour images in 100 classes.

The CIFAR-100 dataset consists of 60000 32x32 colour images in 100 
classes, with 6000 images per class. There are 500 training images
and 100 testing images per class.

Credits:
  Alex Krizhevsky
  Vinod Nair
  Geoffrey Hinton

For more information visit: 
https://www.cs.toronto.edu/~kriz/cifar.html"""

    url = "https://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz"

    samples_dir = env.get('base.samples_dir')

    print("Downloading CIFAR-100 to {}.".format(samples_dir))

    src_dir = download_files([url], dir=env.get('base.cache_dir'))
    path = os.path.join(src_dir, "cifar-100-python.tar.gz")

    tarf = tarfile.TarFile(path, 'r:gz')
    tarf.extractall(src_dir)
    tarf.close()

    shutil.rmtree(src_dir)

    print("Finished downloading CIFAR-100.") 
開發者ID:mme,項目名稱:vergeml,代碼行數:33,代碼來源:cifar_100.py

示例15: download

# 需要導入模塊: import shutil [as 別名]
# 或者: from shutil import rmtree [as 別名]
def download(env):
    """60000 tiny colour images in 10 classes.

The CIFAR-10 dataset consists of 60000 32x32 colour images in 10 
classes, with 6000 images per class. There are 50000 training 
images and 10000 test images.

Credits:
  Alex Krizhevsky
  Vinod Nair
  Geoffrey Hinton

For more information visit: 
https://www.cs.toronto.edu/~kriz/cifar.html"""

    url = "https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz"

    samples_dir = env.get('base.samples_dir')

    print("Downloading CIFAR-10 to {}.".format(samples_dir))

    src_dir = download_files([url], dir=env.get('base.cache_dir'))
    path = os.path.join(src_dir, "cifar-10-python.tar.gz")

    tarf = tarfile.TarFile(path, 'r:gz')
    tarf.extractall(src_dir)
    tarf.close()

    shutil.rmtree(src_dir)

    print("Finished downloading CIFAR-10.") 
開發者ID:mme,項目名稱:vergeml,代碼行數:33,代碼來源:cifar_10.py


注:本文中的shutil.rmtree方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。