网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Python中怎么备份目录

这期内容当中小编将会给大家带来有关Python中怎么备份目录,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

创新互联建站是一家专注网站建设、网络营销策划、微信小程序开发、电子商务建设、网络推广、移动互联开发、研究、服务为一体的技术型公司。公司成立十余年以来,已经为上千余家服务器租用各业的企业公司提供互联网服务。现在,服务的上千余家客户与我们一路同行,见证我们的成长;未来,我们一起分享成功的喜悦。

1. 读取配置文件

配置文件很简单。用的就是txt文件。 格式类似于:

# root:/Users/lichallenger/test_src/  # project:test  # destination:/Users/lichallenger/test_dst/

BTW: 我用的是Mac所以目录格式是这样的。如果你用的是Windows的话请适当修改配置文件。

读文件就是最简单的了。直接用标准库的文件操作模块打开文件,读出全部的配置。一共就三行,所以也不用考虑效率什么的了。

# open config file and read config information  # author: bruce li   class ConfigHandler(object):      #      def __init__(self,config_path):          '''''initializer'''         self.config_path = config_path            #read config infor      def read_config(self):          f = open(self.config_path)           try:              self.all_lines = f.readlines()          except:              raise              else:              f.close()

2. 拷贝目录和目录内容

拷贝目录用了shutil模块。里面有个方法可以直接把目录和目录下的全部内容拷贝到制定的其他目录。

这样就省得搞目录遍历之类的代码了。

# copy dir(s) & file(s) to configured path  # author: bruce li   import shutil   class CopyHandler(object):      #      def __init__(self,src_path,dest_path):          self.src_path = src_path          self.dest_path = dest_path       def move_content(self):          try:              shutil.copytree(self.src_path,self.dest_path)          except:              raise           @staticmethod     def    move_src_content(src, dest):          try:              shutil.copytree(src_path,dest_path)          except:              raise

3. 综合调用

这里用了time模块获取当前时间,然后生成目标文件夹名称的一部分。

外界给python传的系统参数的***个是文件名。这个文件就相当于C#项目里的Program文件一样,

里面会包含一个main函数。虽然这个函数不一定要命名为main。

 # main of dir copy function  import sys  import time  from code_bk_cpy import *  from code_bk_config import *  #print __name__  def main():      config_path = sys.argv[1]         # check if path of configuration path is empty      if (not config_path):          print 'configuration information is needed'         return -1           config_handler = ConfigHandler(config_path)      config_handler.read_config()      config_list = config_handler.all_lines      if len(config_list) != 3:          print 'configuration information is not correct'         return -1         # set source      sep = ':'     current_datetime = time.localtime(time.time())      root_path = config_list[0].split(sep)[1]      prj_name = config_list[1].split(sep)[1]      dst_path = config_list[2].split(sep)[1]      root_path = (root_path + prj_name).replace('\n','')      prj_folder = prj_name + str(current_datetime.tm_year) + str(current_datetime.tm_mon) + \  str(current_datetime.tm_mday) + str(current_datetime.tm_hour) + \  str(current_datetime.tm_min) + str(current_datetime.tm_sec)      dst_path = (dst_path + '/' + prj_folder + '/').replace('\n','')      copy_handler = CopyHandler(root_path,dst_path)      copy_handler.move_content()      print 'content moved' # start main function  print __name__  if __name__ == "__main__":      main()

上述就是小编为大家分享的Python中怎么备份目录了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


当前标题:Python中怎么备份目录
本文来源:http://bjjierui.cn/article/pephjj.html

其他资讯