“Python云存储备份”的版本间的差异
来自CloudWiki
(→复制云端目录(未完)) |
|||
第57行: | 第57行: | ||
− | === | + | ===复制云端目录=== |
− | <nowiki>from os import listdir | + | <nowiki> |
+ | from os import listdir,makedirs | ||
from os.path import join, isfile, isdir | from os.path import join, isfile, isdir | ||
dirs= [] | dirs= [] | ||
− | def | + | dirs_right =[] #云端的目录集合 |
− | ''' | + | dirs_left = [] #本地端的目录集合 |
+ | |||
+ | def GetDirWidthFirst(root_right):#root_right:云端根目录 | ||
+ | '''广度优先遍历,得到云端目录集合''' | ||
#使用列表模拟双端队列,效率稍微受影响,不过关系不大 | #使用列表模拟双端队列,效率稍微受影响,不过关系不大 | ||
− | global dirs | + | global dirs,dirs_right |
− | dirs = [ | + | dirs = [root_right] #dirs:用做广度优先遍历的列表,这个root_right就是列表的第一个元素 |
+ | dirs_right = [root_right] | ||
#如果还有没遍历过的文件夹,继续循环 | #如果还有没遍历过的文件夹,继续循环 | ||
+ | |||
while dirs: | while dirs: | ||
#遍历还没遍历过的第一项 | #遍历还没遍历过的第一项 | ||
第74行: | 第80行: | ||
#如果是文件夹,输出显示后,标记为待遍历项 | #如果是文件夹,输出显示后,标记为待遍历项 | ||
for subPath in listdir(current): | for subPath in listdir(current): | ||
− | print(subPath) | + | #print(subPath) |
− | + | path = join(current,subPath) | |
− | + | #print(path) | |
− | + | #os.path.split(path) | |
− | + | #('D:\\teaching\\自动化运维\\练习', '7.1.py') | |
+ | #current.split("teaching\\") | ||
+ | a,b=current.split("teaching\\") | ||
+ | #print(a,b) | ||
if isdir(path): | if isdir(path): | ||
#print(path) | #print(path) | ||
dirs.append(path) | dirs.append(path) | ||
+ | dirs_right.append(path) | ||
+ | def CreateDirLocal(root_left,root_right): | ||
+ | '''创建本地目录''' | ||
+ | global dirs,dirs_right,dirs_left | ||
− | + | #生成本地目录名 | |
+ | for d in dirs_right: | ||
+ | #print(d) | ||
+ | new_dir =d.replace(root_right,root_left) | ||
+ | dirs_left.append(new_dir) | ||
+ | |||
+ | total,new=0,0 | ||
+ | for d in dirs_left: | ||
+ | #print(d) | ||
+ | total+=1 | ||
+ | if not isdir(d): | ||
+ | print(d,"不存在") | ||
+ | makedirs(d) | ||
+ | new+=1 | ||
+ | print("total=",total,"new=",new) | ||
+ | |||
+ | root_right=r'E:\20200311\teaching\自动化运维' | ||
+ | root_left=r'D:\teaching\自动化运维' | ||
+ | GetDirWidthFirst(root_right) | ||
+ | CreateDirLocal(root_left,root_right) | ||
+ | </nowiki> |
2020年4月5日 (日) 11:04的版本
比较左右两侧文件
import filecmp import os left = r"D:\teaching\人工智能" # 定义左目录 right = r"E:\20200311\teaching\人工智能" #定义右目录 #比较目录 dirobj = filecmp.dircmp(left, right, ['cmp.py']) #目录比较,忽略test.py文件 dirobj.report() print("left_only:") print(dirobj.left_only) # 只在左目录中的文件或目录
复制文件
import shutil for i in dirobj.left_only: left_file=os.path.realpath(i) right_file=os.path.join(right,i) print("左侧发现新文件/目录: "+i) print(left_file) print(right_file) s=input("是否复制?y/n") if s=='y': print("开始复制中...") shutil.copyfile(left_file,right_file) print("复制完毕"+right_file)
广度优先遍历目录
from os import listdir from os.path import join, isfile, isdir def listDirWidthFirst(directory): '''广度优先遍历文件夹''' #使用列表模拟双端队列,效率稍微受影响,不过关系不大 dirs = [directory]#这个directory就是列表的第一个元素 #如果还有没遍历过的文件夹,继续循环 while dirs: #遍历还没遍历过的第一项 current = dirs.pop(0) #遍历该文件夹,如果是文件就直接输出显示 #如果是文件夹,输出显示后,标记为待遍历项 for subPath in listdir(current): path = join(current, subPath) if isfile(path): print(path) elif isdir(path): print(path) dirs.append(path) listDirWidthFirst('.')
复制云端目录
from os import listdir,makedirs from os.path import join, isfile, isdir dirs= [] dirs_right =[] #云端的目录集合 dirs_left = [] #本地端的目录集合 def GetDirWidthFirst(root_right):#root_right:云端根目录 '''广度优先遍历,得到云端目录集合''' #使用列表模拟双端队列,效率稍微受影响,不过关系不大 global dirs,dirs_right dirs = [root_right] #dirs:用做广度优先遍历的列表,这个root_right就是列表的第一个元素 dirs_right = [root_right] #如果还有没遍历过的文件夹,继续循环 while dirs: #遍历还没遍历过的第一项 current = dirs.pop(0) #遍历该文件夹,如果是文件就直接输出显示 #如果是文件夹,输出显示后,标记为待遍历项 for subPath in listdir(current): #print(subPath) path = join(current,subPath) #print(path) #os.path.split(path) #('D:\\teaching\\自动化运维\\练习', '7.1.py') #current.split("teaching\\") a,b=current.split("teaching\\") #print(a,b) if isdir(path): #print(path) dirs.append(path) dirs_right.append(path) def CreateDirLocal(root_left,root_right): '''创建本地目录''' global dirs,dirs_right,dirs_left #生成本地目录名 for d in dirs_right: #print(d) new_dir =d.replace(root_right,root_left) dirs_left.append(new_dir) total,new=0,0 for d in dirs_left: #print(d) total+=1 if not isdir(d): print(d,"不存在") makedirs(d) new+=1 print("total=",total,"new=",new) root_right=r'E:\20200311\teaching\自动化运维' root_left=r'D:\teaching\自动化运维' GetDirWidthFirst(root_right) CreateDirLocal(root_left,root_right)