filecmp模块作用
主要用于比较文件系统上的文件和目录
1、创建演示的目录
import os def mkfile(filename, body=None): # 创建文件,内容没有设置则写入文件名,有传内容则传写指定的内容 with open(filename, 'w') as f: f.write(body or filename) return None 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/common_dir') os.mkdir('dir2/common_dir') # 创建一个文件并且写入指定的内容,并且创建一个软链接到dir2 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') # 更新访问时间和修改时间 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 None 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')