当前位置: 首页 > news >正文

镇江网站建设和优化推广多少钱女生网站开发

镇江网站建设和优化推广多少钱,女生网站开发,潍坊网站建设公司慕枫,十堰专业网站建设Python世界#xff1a;文件自动化备份实践 背景任务实现思路坑点小结 背景任务 问题来自《简明Python教程》中的解决问题一章#xff0c;提出实现#xff1a;对指定目录做定期自动化备份。 最重要的改进方向是不使用 os.system 方法来创建归档文件#xff0c; 而是使用 zip… Python世界文件自动化备份实践 背景任务实现思路坑点小结 背景任务 问题来自《简明Python教程》中的解决问题一章提出实现对指定目录做定期自动化备份。 最重要的改进方向是不使用 os.system 方法来创建归档文件 而是使用 zipfile 或 tarfile 内置的模块来创建它们的归档文件。 ——《简明Python教程》 本文在其第4版示范代码基础上尝试采用内部python自带库zipfile的方式实现功能进行文件压缩备份。 实现思路 文件命名demo_backup_v5.py视为改进的第5版实现除采用自带zipfile的方式还有以下更新 支持外部自定义设参支持自定义压缩文件内目录名称并去除冗余绝对路径 编码思路 指定待备份目录和目标备份路径按日期建立文件夹按时间建立压缩文件 首先进行输入前处理对目录路径进行处理 if len(sys.argv) 3: # 有外部入参取外部输入tobe_backup_dir sys.argv[1] # input dir, sys.argv[0] the name of python filetarget_dir sys.argv[2] # output dircomment_info input(enter a comment information )else: # 无外部入参则内部设定# tobe_backup_dir C:\\Users\\othertobe_backup_dir rE:\roma_data\code_data_in\inboxtarget_dir E:\\roma_data\\code_testcomment_info test demo其次正式进入程序处理函数backup_proc()先判断目标备份目录是否存在如不存在先构造1个。 接着按日期today进行备份文件夹创建按时间now进行压缩文件命名备份。 最后遍历待备份源目录所有文件将其压缩为时间now命名的zip文件中。 # 仅支持单个目录备份 def backup_proc(tobe_backup_dir, target_dir, comment_info):if_not_exist_then_mkdir(target_dir)today target_dir os.sep backup_ time.strftime(%Y%m%d) # 年、月、日now time.strftime(%H%M%S) # 小时、分钟、秒print(Successfully created)# zip命名及目录处理prefix today os.sep nowif len(comment_info) 0:target prefix .zipelse:target prefix _ comment_info.replace( , _) .zipif_not_exist_then_mkdir(today)# 参考链接https://blog.csdn.net/csrh131/article/details/107895772# zipfile打开文件句柄, with打开不用手动关闭with zipfile.ZipFile(target, w, zipfile.ZIP_DEFLATED) as f:for root_dir, dir_list, file_list in os.walk(tobe_backup_dir): # 能遍历子目录所有文件for name in file_list:target_file os.path.join(root_dir, name)all_file_direct_zip Falseif all_file_direct_zip: # 不加内部目录zip_internal_dir_prefix os.sepelse: # 加内部目录zip_internal_dir_prefix comment_info os.sep# 去掉绝对路径指定压缩包里面的文件所在目录结构 arcname zip_internal_dir_prefix target_file.replace(tobe_backup_dir, )# arcname target_file.replace(tobe_backup_dir, )f.write(target_file, arcnamearcname)return测试用例 python外部入参 python demo_backup_v5.py “E:\roma_data\code_data_in\inbox” “E:\roma_data\code_test” python内部入参 python demo_backup_v5.py 本实现的一个缺点是仅支持单一目录备份秉持短小精悍原则如需多目录备份可在以上做加法。 坑点小结 坑点1不要多级目录去除绝对路径 解决zipfile压缩包如何避免绝对路径 坑点2Unable to find python module 运行if not os.path.exists(path_in)报错。 根因python有多个版本3.6运行时不支持需要3.8。 解决Ctrl Shift P输入Select Interpreter指定高版本版本解释器。 参考link1link2 坑点3TypeError: stat: path should be string, bytes, os.PathLike or integer, not list 根因输入的path路径是个list没有拆解开索引访问元素给string输入。 示例实现 # -*- coding: utf-8 -*-Created on 09/03/24 功能文件备份 1、指定待备份目录和目标备份路径 2、按日期建立文件夹 3、按时间建立压缩文件 import os import time import sys import zipfile# 判断该目录是否存在如不存在则创建 def if_not_exist_then_mkdir(path_in):if not os.path.exists(path_in):os.mkdir(path_in)print(Successfully created directory, path_in)# 仅支持单个目录备份 def backup_proc(tobe_backup_dir, target_dir, comment_info):if_not_exist_then_mkdir(target_dir)today target_dir os.sep backup_ time.strftime(%Y%m%d) # 年、月、日now time.strftime(%H%M%S) # 小时、分钟、秒print(Successfully created)# zip命名及目录处理prefix today os.sep nowif len(comment_info) 0:target prefix .zipelse:target prefix _ comment_info.replace( , _) .zipif_not_exist_then_mkdir(today)# 参考链接https://blog.csdn.net/csrh131/article/details/107895772# zipfile打开文件句柄, with打开不用手动关闭with zipfile.ZipFile(target, w, zipfile.ZIP_DEFLATED) as f:for root_dir, dir_list, file_list in os.walk(tobe_backup_dir): # 能遍历子目录所有文件for name in file_list:target_file os.path.join(root_dir, name)all_file_direct_zip Falseif all_file_direct_zip: # 不加内部目录zip_internal_dir_prefix os.sepelse: # 加内部目录zip_internal_dir_prefix comment_info os.sep# 去掉绝对路径指定压缩包里面的文件所在目录结构 arcname zip_internal_dir_prefix target_file.replace(tobe_backup_dir, )# arcname target_file.replace(tobe_backup_dir, )f.write(target_file, arcnamearcname)returnif __name__ __main__:print(start!)# 前处理if len(sys.argv) 3: # 有外部入参取外部输入tobe_backup_dir sys.argv[1] # input dir, sys.argv[0] the name of python filetarget_dir sys.argv[2] # output dircomment_info input(enter a comment information )else: # 无外部入参则内部设定# tobe_backup_dir C:\\Users\\othertobe_backup_dir rE:\roma_data\code_data_in\inboxtarget_dir E:\\roma_data\\code_testcomment_info test demo# 正式运行backup_proc(tobe_backup_dir, target_dir, comment_info)# 正式退出main函数进程以免main函数空跑print(done!)sys.exit()
http://www.w-s-a.com/news/881597/

相关文章:

  • 网站建设完成推广响应式网站设计开发
  • 电商网站用php做的吗网站开发流程可规划为那三个阶段
  • flash网站怎么做音乐停止深圳网站建设金瓷网络
  • 哪个网站可以做房产信息群发怎么做国内网站吗
  • 微商城网站建设公司的价格卖磁铁的网站怎么做的
  • 免费做做网站手机平台软件开发
  • 网站单页做301徐州百度网站快速优化
  • 织梦怎么制作手机网站漳州专业网站建设公司
  • 邓州做网站网络优化概念
  • 查看网站开发phonegap wordpress
  • 网站建设和维护待遇怎样c 做的网站又哪些
  • 淮南网站推广网站开发行业前景
  • 丽水市龙泉市网站建设公司江门手机模板建站
  • 做化妆品注册和注册的网站有哪些wordpress加关键字
  • 四川新站优化php笑话网站源码
  • 外贸类网站酷玛网站建设
  • 合肥网站设计建设南宁网站seo推广优化公司
  • 临沂百度网站7x7x7x7x8黄全场免费
  • 海洋牧场网站建设大良网站设计价格
  • 手机端网站关键字排名北京seo公司哪家好
  • 福建建设培训中心网站网站建站服务公司地址
  • 青岛网站优化快速排名企业网址怎么整
  • 做公司网站用什么系统seo搜索排名优化方法
  • dw怎么做网站标题图标做网站重庆
  • 机场建设相关网站公司官网设计制作
  • 大学网站建设的目标技术支持 优府网络太原网站建设
  • wordpress设置密码访问带提示广州做网站优化哪家专业
  • 如何帮人做网站赚钱西安室内设计公司排名
  • 房产网站建设产品网站域名和邮箱域名
  • 网站建设核心优势seo求职信息