copytree(),什么是参数符号链接和忽略?
copytree(), What are parameters symlink and ignore for?
我是 Python 的新手,所以请多多包涵。
我想写一些东西来将文件从一个目录复制到另一个目录。
我找到了以下问答:Here
在那里,我找到了这个答案:
def copytree(src, dst, symlinks=False, ignore=None):
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
shutil.copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
后来有人为了解决一些问题添加了它:
#!/usr/bin/python
import os
import shutil
import stat
def copytree(src, dst, symlinks = False, ignore = None):
if not os.path.exists(dst):
os.makedirs(dst)
shutil.copystat(src, dst)
lst = os.listdir(src)
if ignore:
excl = ignore(src, lst)
lst = [x for x in lst if x not in excl]
for item in lst:
s = os.path.join(src, item)
d = os.path.join(dst, item)
if symlinks and os.path.islink(s):
if os.path.lexists(d):
os.remove(d)
os.symlink(os.readlink(s), d)
try:
st = os.lstat(s)
mode = stat.S_IMODE(st.st_mode)
os.lchmod(d, mode)
except:
pass # lchmod not available
elif os.path.isdir(s):
copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
我了解到 shutil.copytree() 在目标目录已经存在时会出现问题,这些修订旨在解决这个问题。
我的问题是关于我看到的符号链接和忽略参数,但不了解它们的作用以及它如何解决问题。
我找到了下面的定义,这让我头疼:
symlinks (optional) : This parameter accepts True or False, depending
on which the metadata of the original links or linked links will be
copied to the new tree.
ignore (optional) : If ignore is given, it must be a callable that
will receive as its arguments the directory being visited by
copytree(), and a list of its contents, as returned by os.listdir().
这可能不是您要找的东西,但如果您可以的话,使用 os 的复制功能会更容易编码和理解。这可以通过 python 和 os 库来完成。
def copytree(src,dst):
os.system("cp -r "+src+" "+dst)
此示例假设您使用的是 linux,但它适用于任何 OS,您只需将 'cp -r' 切换为本地复制命令即可。
我是 Python 的新手,所以请多多包涵。 我想写一些东西来将文件从一个目录复制到另一个目录。 我找到了以下问答:Here
在那里,我找到了这个答案:
def copytree(src, dst, symlinks=False, ignore=None):
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
shutil.copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
后来有人为了解决一些问题添加了它:
#!/usr/bin/python
import os
import shutil
import stat
def copytree(src, dst, symlinks = False, ignore = None):
if not os.path.exists(dst):
os.makedirs(dst)
shutil.copystat(src, dst)
lst = os.listdir(src)
if ignore:
excl = ignore(src, lst)
lst = [x for x in lst if x not in excl]
for item in lst:
s = os.path.join(src, item)
d = os.path.join(dst, item)
if symlinks and os.path.islink(s):
if os.path.lexists(d):
os.remove(d)
os.symlink(os.readlink(s), d)
try:
st = os.lstat(s)
mode = stat.S_IMODE(st.st_mode)
os.lchmod(d, mode)
except:
pass # lchmod not available
elif os.path.isdir(s):
copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
我了解到 shutil.copytree() 在目标目录已经存在时会出现问题,这些修订旨在解决这个问题。
我的问题是关于我看到的符号链接和忽略参数,但不了解它们的作用以及它如何解决问题。
我找到了下面的定义,这让我头疼:
symlinks (optional) : This parameter accepts True or False, depending on which the metadata of the original links or linked links will be copied to the new tree.
ignore (optional) : If ignore is given, it must be a callable that will receive as its arguments the directory being visited by copytree(), and a list of its contents, as returned by os.listdir().
这可能不是您要找的东西,但如果您可以的话,使用 os 的复制功能会更容易编码和理解。这可以通过 python 和 os 库来完成。
def copytree(src,dst):
os.system("cp -r "+src+" "+dst)
此示例假设您使用的是 linux,但它适用于任何 OS,您只需将 'cp -r' 切换为本地复制命令即可。