python模块整理25filecmp文件比较linecache选择行shutil复制目录树

一、filecmp模块 文件比较
1、比较两个文件filecmp.cmp()
# echo 0 >/tmp/file1
# echo 1 > /tmp/file2
# echo 0 > /tmp/file3
>>> filecmp.cmp('/tmp/file1','/tmp/file2')
False
>>> filecmp.cmp('/tmp/file1','/tmp/file3')
True
2、比较两个目录文件内容filecmp.dircmp()
# cp /tmp/file1 /tmp/dir1/
# cp /tmp/file* /tmp/dir2/
# echo 3 > /tmp/dir2/file3
# echo 4 > /tmp/dir1/file4
会产生一个对象,这个对象有一些方法
>>> filecmp.dircmp('/tmp/dir1','/tmp/dir2')
<filecmp.dircmp instance at 0x28584c2c>
>>> filecmp.dircmp('/tmp/dir1','/tmp/dir2').same_files
['file.txt', 'file2', 'file1']
>>> filecmp.dircmp('/tmp/dir1','/tmp/dir2').diff_files
['file3']
返回的结果存放在列表里面
这里比较是两边都有的文件内容。
要比较两边包含文件名的对比,可以使用listdir做集合对比
3、比较两个目录文件名[没有比较文件内容]
>>> dirB=set(os.listdir('/tmp/dir2'))
>>> dirA
set(['file3', 'file2', 'file1', 'file.txt', 'file4'])
>>> dirB
set(['file3', 'file2', 'file1', 'file.txt'])
>>> dirA-dirB
set(['file4']


二、linecache选择行
linecache模块
linecache模块允许从任何文件里得到任何的行,并且使用缓存尝试在 内部进行优化,常见的情况是从单个文件读取多行。
这个也用于traceback模块取回包含了格式化的traceback源行。
linecache模块定义了以下函数:

linecache.getline(filename, lineno[, module_globals])
从名为filename 的文件中得到第lineno行。这个函数从不会抛出一个异常–产生错误时它将返回”(换行符将包含在找到的行里)。
如果名为filename的文件没有找到,这个函数将会在模块搜索路 径,sys.path进行搜索。

linecache.clearcache()
清除缓存。如果你不再需要先前从getline()中得到的行,使用这个函数。

linecache.checkcache([filename])
检查缓存的有效性。如果在缓存中的文件在硬盘上发生了变化,并且你需要更新 版本,使用这个函数。如果省略filename,它将检查缓存里的所有条目。>>> import linecache

>>> dir(linecache)
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__',
'cache', 'checkcache', 'clearcache', 'getline', 'getlines', 'os', 'sys', 'updatecache']
>>> print linecache.getlines('/etc/rc.local',3)
['#!/bin/sh\n', '#\n', '# This script will be executed *after* all the other init scripts.\n', "# You can put your own initialization stuff in here if you don't\n", '# want to do the full Sys V style init stuff.\n', '\n', 'touch /var/lock/subsys/local\n', 'sh /root/bin/iptables_init.sh\n']

def getGmtApiLine(filepath,linecount):
return linecache.getline(filepath,linecount)

def getGmtApi(domain,hostname,filename,apiargv,basedir='/etc/puppet/modules/gmt/files'):
filedir=basedir+'/'+domain+'/'+hostname
filepath=os.path.join(filedir,filename)
if apiargv=='publishgmt':
return getGmtApiLine(filepath,3).strip()
elif apiargv=='startgmt':
return getGmtApiLine(filepath,4).strip()
elif apiargv=='stopgmt':
return getGmtApiLine(filepath,5).rstrip()
elif apiargv=='logbegin':
return getGmtApiLine(filepath,6).rstrip()
elif apiargv=='logend':
return getGmtApiLine(filepath,7).rstrip()
elif apiargv=='taskid':
return getGmtApiLine(filepath,8).rstrip()
三、shutil复制目录树
shutil模块
1、复制目录树
shutil.copytree(dir,dir_copy)
>>>os.makedirs('/tmp/test/test1/test2/test3')
>>> os.listdir('/tmp/test/')
['test1']
>>> shutil.copytree('/tmp/test','/tmp/test_copy')
>>> os.listdir('/tmp/test_copy/')
['test1']
2、删除目录树shutil.rmtree('多层目录')
shutil.rmtree('/tmp/test_copy/')
删除的目录可有其他的文件,有点类似rm -rf
OS模块的删除
os.removedirs('/tmp/test/test1/test2')
这个要求所有目录除了目录本身外没有其他文件。
3、移动(改名)shutil.move
目录
shutil.move('/tmp/test','/tmp/test_mv')
文件
shutil.move('/tmp/testfile1','/tmp/testfile2')
os模块的改名方法:os.rename

python读取文件同时输出行号和内容

file = open('file.txt','r')
for (num,value) in enumerate(file):
    print "line number",num,"is:",value
file.close()
原文地址:https://www.cnblogs.com/diege/p/2755375.html