以下内容为学习笔记,内容多数来自Python Cookbook。
2012-12-24 星期一
1.将文件归档到zip文件,并从zip文件中读取数据
'''
Created on Dec 24, 2012
将文件归档到zip文件,并从zip文件中读取数据
@author: liury_lab
'''
# 压缩成zip文件
from zipfile import * #@UnusedWildImport
import os
my_dir = 'd:/中华十大名帖/'
myzip = ZipFile('d:/中华十大名帖.zip', 'w', ZIP_DEFLATED)
for file_name in os.listdir(my_dir):
file_path = my_dir + file_name
print(file_path)
myzip.write(file_path)
myzip.close()
print('finished')
# 从zip 文件中读取数据
# 直接检查一个zip格式的归档文件中部分或所有的文件,同时还要避免将这些文件展开到磁盘上
my_zip = ZipFile('d:/中华十大名帖.zip')
for file_name in my_zip.namelist():
print('File:', file_name, end = ' ')
file_bytes = my_zip.read(file_name)
print('has ', len(file_bytes), ' bytes')
输出:
d:/中华十大名帖/超高清中华十大名帖NO.10 明· 祝允明《草书诗帖》.jpg
d:/中华十大名帖/超高清中华十大名帖NO.2东晋·王羲之《兰亭序》.jpg
d:/中华十大名帖/超高清中华十大名帖NO.3 唐·欧阳询《仲尼梦奠帖》.jpg
d:/中华十大名帖/超高清中华十大名帖NO.4 唐·颜真卿《祭侄文稿》.jpg
d:/中华十大名帖/超高清中华十大名帖NO.5 唐·怀素《自叙帖》.jpg
d:/中华十大名帖/超高清中华十大名帖NO.6 北宋·苏轼《黄州寒食帖》.jpg
d:/中华十大名帖/超高清中华十大名帖NO.7 北宋.米芾《蜀素帖》.jpg
d:/中华十大名帖/超高清中华十大名帖NO.8 北宋·徽宗赵佶 《草书千字文》.jpg
d:/中华十大名帖/超高清中华十大名帖王献之《中秋帖》.jpg
d:/中华十大名帖/超高清中华十大名帖王羲之 时雪快晴帖.jpg
finished
File: 中华十大名帖/超高清中华十大名帖NO.10 明· 祝允明《草书诗帖》.jpg has 5739261 bytes
File: 中华十大名帖/超高清中华十大名帖NO.2东晋·王羲之《兰亭序》.jpg has 8430748 bytes
File: 中华十大名帖/超高清中华十大名帖NO.3 唐·欧阳询《仲尼梦奠帖》.jpg has 4203138 bytes
File: 中华十大名帖/超高清中华十大名帖NO.4 唐·颜真卿《祭侄文稿》.jpg has 8426200 bytes
File: 中华十大名帖/超高清中华十大名帖NO.5 唐·怀素《自叙帖》.jpg has 4930870 bytes
File: 中华十大名帖/超高清中华十大名帖NO.6 北宋·苏轼《黄州寒食帖》.jpg has 4427979 bytes
File: 中华十大名帖/超高清中华十大名帖NO.7 北宋.米芾《蜀素帖》.jpg has 17778721 bytes
File: 中华十大名帖/超高清中华十大名帖NO.8 北宋·徽宗赵佶 《草书千字文》.jpg has 7720300 bytes
File: 中华十大名帖/超高清中华十大名帖王献之《中秋帖》.jpg has 1033725 bytes
File: 中华十大名帖/超高清中华十大名帖王羲之 时雪快晴帖.jpg has 678387 bytes
2012-12-25 星期二
2.处理字符串中的zip文件
# 程序接受一个字符串,其内容是一个zip文件,需要读取这个zip文件中的信息
import zipfile
class zip_string(zipfile.ZipFile):
def __init__(self, data_string):
zipfile.ZipFile.__init__(self, data_string)
zstr = zip_string('d:/中华十大名帖.zip')
for file_name in zstr.namelist():
print('File:', file_name)
输出:
File: 中华十大名帖/超高清中华十大名帖NO.10 明· 祝允明《草书诗帖》.jpg
File: 中华十大名帖/超高清中华十大名帖NO.2东晋·王羲之《兰亭序》.jpg
File: 中华十大名帖/超高清中华十大名帖NO.3 唐·欧阳询《仲尼梦奠帖》.jpg
File: 中华十大名帖/超高清中华十大名帖NO.4 唐·颜真卿《祭侄文稿》.jpg
File: 中华十大名帖/超高清中华十大名帖NO.5 唐·怀素《自叙帖》.jpg
File: 中华十大名帖/超高清中华十大名帖NO.6 北宋·苏轼《黄州寒食帖》.jpg
File: 中华十大名帖/超高清中华十大名帖NO.7 北宋.米芾《蜀素帖》.jpg
File: 中华十大名帖/超高清中华十大名帖NO.8 北宋·徽宗赵佶 《草书千字文》.jpg
File: 中华十大名帖/超高清中华十大名帖王献之《中秋帖》.jpg
File: 中华十大名帖/超高清中华十大名帖王羲之 时雪快晴帖.jpg
2012-12-26 星期三
3.将文件树归档到一个压缩的tar文件
# 将一个文件树中的所有文件和子目录归档到一个tar归档文件,然后压缩
import tarfile, os
# compression表示压缩算法,gz表示gzip颜色,bz2表示bzip2压缩,空字符串表示不压缩
# folder_to_backup: 要归档的文件夹
# dest_folder 表示目标文件夹
def make_tar(folder_to_backup, dest_folder, compression = 'bz2'):
# dest_ext 表示扩展名
if compression:
dest_ext = '.' + compression
else:
dest_ext = ''
arc_name = os.path.basename(folder_to_backup)
# dest_name 为目标文件名,dest_path 为目标文件路径
dest_name = '%s.tar%s' % (arc_name, dest_ext)
dest_path = os.path.join(dest_folder, dest_name)
# 压缩方法决定了open的第二个参数是 "w", 或"w:gz", 或"w:bz2"
if compression:
dest_cmp = ':' + compression
else:
dest_cmp = ''
out = tarfile.TarFile.open(dest_path, 'w' + dest_cmp)
out.add(folder_to_backup, arc_name)
out.close()
return dest_path
dest_path = make_tar('d:/8 file_system', 'd:/')
print(dest_path)
2012-12-27 星期四
4.遍历目录树
import os, fnmatch
# 检查一个目录,后者某个包含子目录的目录树,并根据某种模式迭代所有文件
# patterns如:*.html,若大小写敏感可写*.[Hh][Tt][Mm][Ll]
# single_level 为True表示只检查第一层
# yield_folders 表示是否显示子目录,为False只遍历子目录中的文件,但不返回字母名
def all_files(root, patterns='*', single_level=False, yield_folders=False):
# 将模式从字符串中取出放入列表中
patterns = patterns.split(';')
for path, subdirs, files in os.walk(root):
if yield_folders:
files.extend(subdirs)
files.sort()
for name in files:
for pattern in patterns:
if fnmatch.fnmatch(name, pattern):
yield os.path.join(path, name)
break
if single_level:
break
for file in all_files('d:\\pm', '*.s;*.c', False, False):
print(file)
输出:
d:\pm\pm\bootsect.s
d:\pm\pm\fix_img.c
d:\pm\pm\load.s
d:\pm\read_one_sect\read_one_sector.s
d:\pm\read_one_sect\write_data.c
d:\pm\read_sectors\read_sectors.s
d:\pm\read_sectors\write_data.c
2012-12-28 星期五
5.在目录树中改变文件扩展名
# 在一个目录的子树中重命名一系列文件:指定某一指定类型的文件的扩展名,改成另一种扩展名
import os
def swap_extensions(dir, before, after):
if before[:1] != '.':
before = '.' + before
the_len = -len(before)
if after[:1] != '.':
after = '.' + after
for path, sub_dirs, files in os.walk(dir):
for old_file in files:
if old_file[the_len:] == before:
old_file = os.path.join(path, old_file)
new_file = old_file[:the_len] + after
os.rename(old_file, new_file)
swap_extensions('d:\\pm', '.s', '.asm')
2012-12-29 星期六
6.从指定的搜索路径寻找文件
# 给定一个搜索路径,根据这个路径请求和请求的文件名,找到第一个符合要求的文件
import os
def search_file(file_name, search_path, pathsep = os.pathsep):
for path in search_path.split(pathsep):
candidate = os.path.join(path, file_name)
if os.path.isfile(candidate):
return os.path.abspath(candidate)
return None
search_path = 'd:\\pm\\pm'
find_file = search_file('babyos.img', search_path)
if find_file:
print("File 'babyos.img' found at %s" % find_file)
else:
print("File 'babyos.img' not found")
2012-12-30 星期日
7.根据指定的搜索路径和模式寻找文件
# 给定一个搜索路径,需要在此目录中找出所有符合匹配模式的文件
import glob, os
def all_files(pattern, search_path, pathsep = os.pathsep):
for path in search_path.split(pathsep):
for match in glob.glob(os.path.join(path, pattern)):
yield match
print(type(all_files('*.s', 'd:\\pm\\pm')))
print(all_files('*.s', 'd:\\pm\\pm').__next__())
for match in all_files('*.s', 'd:\\pm\\pm'):
print(match)