我目前正在尝试获取Windows卷上的所有文件,以便复制某些文件。从一个文件夹复制到另一个文件夹可以很好地工作,但是当尝试列出目录然后遍历找到的卷文件时,“系统卷信息”的访问被拒绝例外引起了我的欢迎。
如何在循环中忽略/跳过呢?
我使用的是递归函数,第一次使用卷本身的根路径调用它。
def list_all(path):
files = os.listdir(path)
for file in files:
low_path = os.path.join(path, file)
if os.path.isdir(low_path):
list_all(low_path)
else:
# shutil.copy()
您可以添加一个try/except
块
def list_all(path):
files = os.listdir(path)
try:
files.remove("System Volume Information")
except:
print("System Volume Information not present in this directory")
for file in files:
low_path = os.path.join(path, file)
if os.path.isdir(low_path):
list_all(low_path)
else:
# shutil.copy()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句