python写的目录同步小程序
2009-01-30 14:20
453 查看
我需要定期将电脑的上的资料备份到移动硬盘,主要是一些照片,为了方便用python写了一段脚本:
# -*- coding: utf-8 -*- #目录同步工 #使用方法: #SynchRoot sourcepath destinationpath import os import sys import shutil import getopt #是否显示操作 _verbos = 0 #检查源目录的文件及文件夹 def SynchPath(src, dst): #如果目录不存在,则创建目录 if not os.path.exists(dst): os.mkdir(dst) names = os.listdir(src) for name in names: srcname = os.path.join(src, name) dstname = os.path.join(dst, name) try: if os.path.isdir(srcname): #目录 #如果目录不存在,则创建目录 if not os.path.exists(dstname): os.mkdir(dstname) SynchPath(srcname, dstname) elif os.path.isfile(srcname): #查看目的目录下是否有这个文件,如果没有,则拷过去 if not os.path.exists(dstname): if _verbos == 1: print "copying " + srcname + " to " + dstname shutil.copy2(srcname, dstname) except (IOError, os.error), why: if _verbos == 1: print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why)) def usage(): print ''' usage: python SynchRoot.py -s srcpath -d dstpath -v sample: python SynchRoot.py -s E://重要 -d E://test ''' def Main(argv): srcpath = 'E://重要' dstpath = 'E://test' try: opts, args = getopt.getopt(argv, "hs:d:v", ["help", "source", "target", "verbos"]) except getopt.GetoptError: usage() sys.exit(2) for opt, arg in opts: if opt in ("-h", "--help"): usage() sys.exit() elif opt in ("-s", "--source"): srcpath = arg elif opt in ("-d", "--target"): dstpath = arg elif opt in ("-v", "--verbos"): global _verbos _verbos = 1 if not os.path.exists(srcpath): print "错误:源路径必须存在!" else: print "正在同步: " + srcpath + " >>>>>>> " + dstpath SynchPath(srcpath, dstpath) print "已完成同步" #执行操作 Main(sys.argv[1:])
相关文章推荐
- python写的目录同步小程序收藏
- Python获取程序运行目录和脚本目录
- python实现的ftp自动上传下载程序(支持目录递归操作)----转
- python批量同步web服务器代码核心程序
- python的字符串操作、启动程序、查看进程。目录。
- python程序使用pyinstaller打包成单一.exe文件后,如何的到.exe文件所在目录?
- IOS的app程序中的文件目录的功能和使用详细说明 - 包括itune同步的部分
- python批量同步web服务器代码核心程序
- Python学习写一小程序,从某一目录下所以文件中查询某字段,并显示文件名和所在行
- 每天一个python小程序 007有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
- 让你Python程序软件目录更规范化
- python监听本地目录,并将目录下的文件同步到远端服务器
- python中获取指定目录下所有文件名列表的程序
- python中获取指定目录下所有文件名列表的程序
- python爬虫程序,由根目录一直爬各个网址
- python获取程序运行目录和脚本目录
- 自动同步2个目录python脚本
- 代码风格.Python-整体风格.0002.让你Python程序软件目录更规范化?
- Python:删除用户home目录下的PNG文件的小程序。
- python pip通过本地目录安装相关程序包