该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
def copy_dir(sourceDir,destDir):
for file in os.listdir(sourceDir):
sourceFile = os.path.join(file)
destFile = os.path.join(file)
#此处打印的结果全是False 不管是文件还是文件夹
print sourceFile,os.path.isfile(sourceFile)\
,os.path.isdir(sourceFile),os.path.realpath(sourceFile)
if(os.path.isfile(sourceFile)):
print sourceFile
os.mkdirs(destDir)
if not os.path.exist(destDir):
os.mkdirs(destDir)
if not os.path.exist(destDir) \
or (os.path.exist(destFile) \
and (os.path.getsize(sourceFile) \
!= os.path.getsize(destFile) )):
open(destFile,"w+").write(open(sourceFile,"rb").read())
elif os.path.isdir(sourceFile):
copy_dir(sourceFile,destFile)
print "Copy End."
#main函数
if __name__ == "__main__":
copy_dir("E:\\Pythonwork\\test_1\\","./dest_1/")
本文介绍了一个简单的Python脚本,用于递归地复制文件夹及其内容。该脚本能够处理文件及子文件夹,并检查目标文件是否已存在且大小一致,以避免重复复制。尽管存在一些逻辑错误,例如未正确使用`os.path.join()`来组合路径,但核心功能依然明确。
598

被折叠的 条评论
为什么被折叠?



