python 复制,移动,整个文件夹含子文件夹及子文件,无论递归多少层
import os
import shutil
import sys
if 'win' in sys.platform:
ge = '\\'
else:
ge = '/'
def mymovefile(srcpath, dstpath):
if not os.path.exists(dstpath):
os.makedirs(dstpath)
for roots, dirs, files in os.walk(srcpath):
for dir in dirs:
Subfolders = dstpath + ge + srcpath.split(ge)[-1] + ge + (roots + ge + dir).split(srcpath.split(ge)[-1])[1]
print(Subfolders)
if not os.path.exists(Subfolders):
os.makedirs(Subfolders)
for file in files:
srcfile = roots + ge + file
print(srcfile)
dst_file_path = dstpath + ge + srcpath.split(ge)[-1] + ge + srcfile.split(srcpath.split(ge)[-1])[1]
shutil.move(srcfile, dst_file_path)
print("move %s -> \r\n%s" % (srcfile, dst_file_path))
def mycopyfile(srcpath, dstpath):
if not os.path.exists(dstpath):
os.makedirs(dstpath)
for roots, dirs, files in os.walk(srcpath):
for dir in dirs:
Subfolders = dstpath + ge + srcpath.split(ge)[-1] + ge + (roots + ge + dir).split(srcpath.split(ge)[-1])[1]
print(Subfolders)
if not os.path.exists(Subfolders):
os.makedirs(Subfolders)
for file in files:
srcfile = roots + ge + file
print(srcfile)
dst_file_path = dstpath + ge + srcpath.split(ge)[-1] + ge + srcfile.split(srcpath.split(ge)[-1])[1]
shutil.copy(srcfile, dst_file_path)
print("copy %s -> \r\n%s" % (srcfile, dst_file_path))