python脚本实现文件夹增量复制
2016-03-09 16:24
603 查看
利用os包os.path.walk() 方法递归的遍历路径和文件
# -*- coding: utf-8 -*- """ Created on Tue Mar 08 10:29:52 2016 @author: Felix """ import os import sys def createIfNotExists(path): if os.path.exists(path): print "path already exists!" else: os.mkdir(path) print "dir created" def copy_file(src,tg): if os.path.exists(tg): print "path already exists!" else: os.system("cp %s %s"%(src,tg)) print "file created" def check_dirs(src_dir,tg_dir): for root,dirs,files in os.walk(src_dir): for d in dirs: src_dirname=os.path.join(root,d) tg_dirname=src_dirname.replace(src_dir,tg_dir) createIfNotExists(tg_dirname) return True def copy_all(src_dir,tg_dir): for root,dirs,files in os.walk(src_dir): for f in files: src_filepath=os.path.join(root,f) tg_filepath=src_filepath.replace(src_dir,tg_dir) copy_file(src_filepath,tg_filepath) return True if __name__=='__main__': src_dir= sys.argv[1] tg_dir = sys.argv[2] # src_dir='D:/py/test' # tg_dir='D:/py/test2' check_dirs(src_dir,tg_dir) copy_all(src_dir,tg_dir)
相关文章推荐
- python学习之easygui 库简介
- python匿名函数lambda函数
- 【python】编程语言入门经典100例--31
- Python学习第一天--关于编码格式
- Java学习篇之---Eclipse中运行python程序
- [LeetCode]题解(python):113-Path Sum II
- [LeetCode]题解(python):112-Path Sum
- Python装饰器
- 修改上一例,改为多线程执行
- [LeetCode]题解(python):111-Minimum Depth of Binary Tree
- python 异常
- Python学习(三) 八大排序算法的实现(下)
- [LeetCode]题解(python):110-Balanced Binary Tree
- [LeetCode]题解(python):109-Convert Sorted List to Binary Search Tree
- python 常用高效代码写法集锦
- [LeetCode]题解(python):108-Convert Sorted Array to Binary Search Tree
- Python windows下安装scipy 和numpy
- 如何成为Python高手
- 【翻译】Leapmotion-python开发官方文档(7)
- python只引入模块并不能直接调用该模块的函数