Python学习笔记三

花了大半天的时间,结合文档中的例子,自己写了一个Windows下使用WinRAR压缩的功能。代码如下:

# coding=utf-8

# 备份文件与压缩:gzip、zipfile、rar

import os,glob 
import time
import zipfile
import gzip

# 三、使用rar压缩文件
# 用WinRAR的rar命令压缩文件,首先要安装有WinRAR且设置WinRAR到环境变量的路径Path中。
# 验证环境是否正常,可在cmd模式下输入:rar a D:/Python27/test D:/Python27/test,能够正常执行就OK。
# 我第一次验证时cmd可以执行成功,但是跑下面的代码不成功。最后重启了电脑,再执行就成功了。

source = r'D:\Python27\test'
target_dir = 'D:\\Python27\\'

# The current day is the name of the subdirectory in the main directory
today = target_dir + time.strftime('%Y%m%d')

# The current time is the name of the rar archive
now = time.strftime('%H%M%S')
print now

# Take a comment from the user to create the name of the rar file
comment = raw_input('Enter a comment --> ')
if len(comment) == 0: # check if a comment was entered
    target = today + os.sep + now + '.rar' # The name of the rar file
else:
    target = today + os.sep + now + '_' + \
             comment.replace(' ', '_') + '.rar'
    # Notice the backslash!


# Create the subdirectory if it isn't already there
if not os.path.exists(today):
    os.mkdir(today) # make directory
    print 'Successfully created directory', today   


# Use the rar command to put the files in a rar archive
rar_command = "rar a %s %s" %(target,''.join(source)) # join方法把source列表转换为字符串
print rar_command

# Run the backup
if os.system(rar_command) == 0:
    print 'Successful backup', target
    
else:
    print 'Failed!'

 

执行结果:

>>> ================================ RESTART ================================
>>>
content:  Lots of content here
Lots of content here
Successful backup to test2.zip
data.py (2014, 7, 25, 14, 27, 10) 3384 1620
file.py (2014, 7, 22, 17, 57, 52) 1482 640
NEWS.txt (2013, 5, 15, 22, 41, 12) 359882 130523
160803
Enter a comment --> added new comment
rar a D:\Python27\20140730\160803_added_new_comment.rar D:\Python27\test
Successful backup D:\Python27\20140730\160803_added_new_comment.rar
>>>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python学习笔记PDF是一种学习Python编程语言的资料形式,它包含了Python的基本语法、数据类型、流程控制、函数、模块、面向对象编程、异常处理等相关内容。以下是关于Python学习笔记PDF的详细内容说明: 1. 基本语法:Python学习笔记PDF中,首先介绍了Python的基本语法,例如如何定义变量、数据类型的使用(包括整数、浮点数、字符串、列表、元组、字典等),以及如何进行算术运算、比较运算和逻辑运算。 2. 流程控制:Python学习笔记PDF中,进一步介绍了流程控制的知识,包括条件判断和循环控制。条件判断主要是通过if语句进行判断执行不同的代码块,而循环控制则通过while循环和for循环来实现重复执行一段代码。 3. 函数:Python学习笔记PDF中,对函数的概念和使用进行了详细的解释。函数是代码的封装和组织方式,通过定义函数可以提高代码的重用性和可读性。学习者将了解到如何定义函数、调用函数、函数参数的传递以及函数返回值的使用。 4. 模块:Python学习笔记PDF中,介绍了Python中的模块和包的概念。模块是一组函数、类或变量的集合,以.py文件的形式存在,可以被其他程序调用和使用。学习者将学习如何导入模块、使用模块中的函数和变量。 5. 面向对象编程:Python学习笔记PDF中,对面向对象编程进行了系统的讲解。面向对象编程是一种以对象为基本单位的编程思想,通过定义类和对象,可以更好地组织和管理代码。学习者将了解如何定义类、创建对象、封装、继承和多态的使用。 6. 异常处理:Python学习笔记PDF中,对异常处理进行了介绍。异常是程序在运行过程中出现的错误,通过捕获和处理异常,可以更好地控制程序的执行流程并提高程序的健壮性。学习者将了解到如何使用try-except语句来捕获和处理异常。 总之,Python学习笔记PDF是一份全面而详细的学习Python编程语言的资料,通过学习该资料,学习者将获得扎实的Python编程基础,并能够运用Python进行简单的程序开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值