Python3filecmp模块测试比较文件原理解析-创新互联
1.filecmp比较文件

filecmp模块提供了一些函数和一个类来比较文件系统上的文件和目录。
1.1 示例数据
使用下面代码创建一组测试文件。
import os
def mkfile(filename, body=None):
with open(filename, 'w') as f:
f.write(body or filename)
return
def make_example_dir(top):
if not os.path.exists(top):
os.mkdir(top)
curdir = os.getcwd()
os.chdir(top)
os.mkdir('dir1')
os.mkdir('dir2')
mkfile('dir1/file_only_in_dir1')
mkfile('dir2/file_only_in_dir2')
os.mkdir('dir1/dir_only_in_dir1')
os.mkdir('dir2/dir_only_in_dir2')
os.mkdir('dir1/common_dir')
os.mkdir('dir2/common_dir')
mkfile('dir1/common_file', 'this file is the same')
os.link('dir1/common_file', 'dir2/common_file')
mkfile('dir1/contents_differ')
mkfile('dir2/contents_differ')
# Update the access and modification times so most of the stat
# results will match.
st = os.stat('dir1/contents_differ')
os.utime('dir2/contents_differ', (st.st_atime, st.st_mtime))
mkfile('dir1/file_in_dir1', 'This is a file in dir1')
os.mkdir('dir2/file_in_dir1')
os.chdir(curdir)
return
if __name__ == '__main__':
os.chdir(os.path.dirname(__file__) or os.getcwd())
make_example_dir('example')
make_example_dir('example/dir1/common_dir')
make_example_dir('example/dir2/common_dir')另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章题目:Python3filecmp模块测试比较文件原理解析-创新互联
链接分享:http://www.jxjierui.cn/article/cspcid.html


咨询
建站咨询
