Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用shutil忽略多个相同的子目录_Python_Copy_Shutil - Fatal编程技术网

Python 使用shutil忽略多个相同的子目录

Python 使用shutil忽略多个相同的子目录,python,copy,shutil,Python,Copy,Shutil,假设我有一个名为local的文件夹,我想使用python3.7和shutil库对其进行递归复制。local文件夹始终具有相同的内部结构 . ├── local/ ├── A/ | ├── img/ | | ├── 1.jpg | | ├── ... | | └── n.jpg | | | └── txt/ | ├── text_1.txt |

假设我有一个名为
local
的文件夹,我想使用
python3.7
shutil
库对其进行递归复制。
local
文件夹始终具有相同的内部结构

.
├── local/
    ├── A/
    |   ├── img/     
    |   |    ├── 1.jpg
    |   |    ├── ...
    |   |    └── n.jpg     
    |   |
    |   └── txt/
    |        ├── text_1.txt
    |        ├── ...
    |        └── text_n.txt
    ├── B/
    .   ├── img/     
    .   |    ├── 1.jpg
    .   |    ├── ...
        |    └── n.jpg     
        |
        └── txt/
             ├── text_1.txt
             ├── ...
             └── text_n.txt
使用
shutil
库及其
copy
方法,如果要获得以下
remote
结构(递归忽略每个子文件夹中的
img
文件夹),如何编写
ignore\u patterns
参数:


我天真地尝试了
shutil.copytree('local','remote',ignore=shutil.ignore_patterns('local/*/img'))
,但没有成功。

从您发布的代码中可以看出,您的
local
remote
目录与您的脚本相关,那么以下内容如何:

from shutil import copytree, ignore_patterns

# also use an absolute path below, prefix with r'<Absolute Path...>'
source = 'local' 
destination = 'remote'

copytree(
    src=source,
    dst=destination,
    ignore=ignore_patterns('img')
)
从shutil导入copytree,忽略\u模式
#也使用下面的绝对路径,前缀为r“”
source='local'
目的地='remote'
复制树(
src=源,
dst=目的地,
忽略=忽略模式(“img”)
)
shutil
模块已经包含了
ignore\u patterns()
函数,因此不需要创建自己的函数

在路径前面加上
r'
将创建一个原始字符串,该字符串将反斜杠视为文字字符,因此不需要转义它们

from shutil import copytree, ignore_patterns

# also use an absolute path below, prefix with r'<Absolute Path...>'
source = 'local' 
destination = 'remote'

copytree(
    src=source,
    dst=destination,
    ignore=ignore_patterns('img')
)