python学习笔记-day4(解决实际问题)
1.version1
import os:导入os模块,用于与操作系统进行交互。
import time:导入time模块,用于处理时间相关的操作。
第三行中,保留中括号,会将source定义为一个包含单个元素的列表。当你需要处理多个源路径时,使用列表结构更加方便和一致。如
source = ['/Users/swa/notes', '/Users/swa/documents']
for s in source:
print(s)
source = '/Users/swa/notes' 此种表达,将source定义为一个单一的字符串。
下面的os.sep 是 Python 的 os 模块中的一个属性,它表示当前操作系统使用的路径分隔符。在这里通过time.strftime() 函数来创建。文件名将以 .zip 作为扩展名,并存储在 target_dir 目录中。
我们使用连接(Concatenates)字符串的加法( + )运算符来创建目标 zip 文件的文件名,也就是说,它将两个字符串连接到一起并返回一个新的字符串。
随后,我们终于可以运行这一使用了 os.system 函数的命令,os.system(zip_command) 是一个 Python 函数,它可以在 Python 脚本中执行系统命令。这一函数可以使命令像是从系统中运行的。也就是说,从 shell 中运行的——如果运行成功,它将返回 0 ,如果运行失败,将返回一个错误代码。
2.version2
优化:分层的形式予以存储;文件名能够更短;只有当天进行了备份才会创建相应的目录,独立的目录能够帮助你快速地检查每天是否都进行了备份
import os
import time
source = ['/Users/swa/notes'] #需要备份的目录
target_dir = '/Users/swa/backup' #文件备份的目标目录
if not os.path.exists(target_dir):
os.mkdir(target_dir)
# 4. 将当前日期作为主备份目录下的子目录名称
today = target_dir + os.sep + time.strftime('%Y%m%d')
# 将当前时间作为 zip 文件的文件名
now = time.strftime('%H%M%S')
# zip 文件名称格式
target = today + os.sep + now + '.zip'
# 如果子目录尚不存在则创建一个
if not os.path.exists(today):
os.mkdir(today)
print('Successfully created directory', today)
zip_command = 'zip -r {0} {1}'.format(target,
' '.join(source))
print('Zip command is:')
print(zip_command)
print('Running:')
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')
join的用法
在 Python 中,join() 是一个字符串方法,用于将序列(如列表、元组等)中的元素连接成一个单一的字符串。join() 方法通过在每个元素之间插入指定的分隔符来实现连接。
join() 方法返回一个新的字符串,该字符串是将序列中的所有元素使用指定的分隔符连接起来的结果。
上面的join代码返回的即是:
zip -r /Users/swa/backup/20140329/073201.zip /Users/swa/notes
注意:所有元素必须是字符串类型:join() 方法要求序列中的所有元素都是字符串。如果序列中包含非字符串类型的元素,会引发 TypeError
字符串与非字符串的区别
字符串类型(String)是一种用于表示文本数据的基本数据类型。它通常由一系列字符组成,可以使用单引号、双引号或其他成对的符号来定义。
字符串的特点:
可包含各种字符:包括字母、数字、符号、空格等。
支持操作:如拼接 (+)、重复 (*)、切片、查找、替换等。
不可变性:无法直接修改字符串中的某个字符,只能创建新的字符串。
什么是字符串类型?
# 使用单引号定义字符串
single_quoted = 'Hello, World!'
# 使用双引号定义字符串
double_quoted = "Hello, Python!"
# 使用三引号定义多行字符串
multi_line = """这是一个
多行字符串
示例。"""
print(single_quoted) # 输出: Hello, World!
print(double_quoted) # 输出: Hello, Python!
print(multi_line) # 输出: 这是一个
# 多行字符串
# 示例。
什么不是字符串类型?
数字类型如整数或者浮点数。布尔类型。列表。元组。集合。字典。
进一步解释(感觉还挺有用的)
示例 1:字符串与整数的区别
虽然字符串 "123" 和整数 123 看起来相似,但它们的类型不同,用途也不同。例如,字符串可以用于表示文本信息,而整数用于数学运算。
# 字符串
s = "123"
print(type(s)) # 输出: <class 'str'>
# 整数
n = 123
print(type(n)) # 输出: <class 'int'>
示例 2:字符串操作与非字符串操作
# 字符串拼接
greeting = "Hello, " + "World!"
print(greeting) # 输出: Hello, World!
# 数字相加
a = 5
b = 10
sum = a + b
print(sum) # 输出: 15
尝试将字符串和数字直接相加会导致错误:
version3
在上一版基础上,针对压缩文件的文件名,可以自定义了。
知识点:
1.'%Y%m%d' 注意加上引号
2.注意input的用法。动态赋予变量的值。
3.python的len函数返回对象的长度或元素的数量,结果是一个整数。
如果对象是字符串,则返回字符串的长度,包括空格。如果对象是元组(tuple)、列表(list)、字典(dictionary) 和 集合(set)等,则返回的是其元素的个数。
4.replace
在这段代码中,replace 是一个字符串方法,用于替换字符串中的子字符串。具体来说,replace 方法的语法如下:
str.replace(old, new, count)
count(可选):指定替换操作的次数。如果不提供,默认替换所有匹配的子字符串。
zip_command = 'zip -r {0} {1}'.format(target,' '.join(source))
import os
import time
source = ['/Users/swa/notes']
target_dir = '/Users/swa/backup'
if not os.path.exists(target_dir):
os.mkdir(target_dir)
today = target_dir + os.sep + time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')
comment = input('Enter a comment --> ')
if len(comment) == 0:
target = today + os.sep + now + '.zip'
else:
target = today + os.sep + now + '_' + \
comment.replace(' ', '_') + '.zip'
if not os.path.exists(today):
os.mkdir(today)
print('Successfully created directory', today)
zip_command = 'zip -r {0} {1}'.format(target,
' '.join(source))
print('Zip command is:')print(zip_command)print('Running:')
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')