博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 初体验(四)
阅读量:7244 次
发布时间:2019-06-29

本文共 1580 字,大约阅读时间需要 5 分钟。

学习语言最好的办法就是实践,这句话一点都不假!

今天完成了一个Python的小脚本,但是其中却遇到了一些意想不到的困难,最终通过上网搜索资料终于将这些问题解决了,对于我这个连Python都还不是太懂的菜鸟来说,真的很不容易啊!

学到了什么?

  1. python中r的用法,r'str'表示raw string,既忽略转义字符。因为和windows不一样,python中认为\就是转义字符escape sequences的标志。
  2. python中提取系统时间,以及将其转化成字符串的方法。time.strftime()。
  3. 将list转化成str的方法,s.join(list),其中s是list不同元素转换成string后中间的空格符。
  4. 7z压缩的方法中 如果加上-t7z 这种方法的压缩效果更好
  5. 解决zip_command不能调用7z的两种办法:

a. 通过计算机→属性→系统保护→高级→环境变量→系统变量  path 修改环境变量,添加7z的path

b. 直接在zip_command命令中添加7z的路径,但是需要注意由于Program Files有空格,Python不能识别它,所以需要将zip_command改成:

    zip_command = r'D:\Progra~1\7-Zip\7z' + " a %s %s" % ( target, ' '.join(source) )

    造成这种原因是,dos下只支持8.3文件名规格,多过的都会以~1结尾

   

完成的小脚本是实现系统文件的备份。

#!F:/document/python

# Filename: backup_ver1.py
import os
import time
##import sys
##
##os.sys.path.append(r'D:\Progra~1\7-Zip')
#1. the files and derectories to be backed up are specified in a list.
source = [r'f:\document\python\a.txt',r'f:\document\html']
#2. the backup must be stored in a main backup directory.
target_dir = r'f:\document\backup\\'
#3. the files are backed up into a zip file.
#4. the name of the zip archive is the current date and time
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'
#5. put files in a zip archive
##zip_command = "rar a -r %s %s" % ( target, ' '.join(source) )
##zip_command = r'D:\Progra~1\7-Zip\7z' + " a %s %s" % ( target, ' '.join(source) )
zip_command = "7z a %s %s" % ( target, ' '.join(source) )
#  run the backup
if os.system(zip_command) == 0:
    print 'Successful backup to', target
else:
    print 'backup failed'

转载于:https://www.cnblogs.com/bovine/archive/2011/11/25/2262535.html

你可能感兴趣的文章
C#集合
查看>>
ssh 登陆redhat linux时中文显示乱码解决方法
查看>>
【Linux】 静态函数库设计
查看>>
ecshop_2.7.2_flow.php_sql注入漏洞利用工具
查看>>
Yii2手动安装第三方扩展
查看>>
个人作业数组续
查看>>
(转载博文)MFC 窗口句柄获取
查看>>
HAProxy详解(三)
查看>>
脚本初步
查看>>
svn 常见问题记录
查看>>
IIS 部署 python web框架 Flask
查看>>
MariaDB基础(2)—管理篇
查看>>
教你如何用 lib-flexible 实现移动端H5页面适配
查看>>
mysql5.6中的reset slave和reset master 参数的介绍和应用
查看>>
MySQL 命令行工具mycli
查看>>
如何使用LVM卷管理Linux系统中的磁盘
查看>>
我的友情链接
查看>>
linux下ntp服务配置
查看>>
StringBuffer 和 StringBiulder的区别
查看>>
Android新浪微博下拉刷新(最新消息显示在最上面)
查看>>