python判断文件是否存在目录中

##支持排除文件和目录
#
!/usr/bin/python #coding:utf-8 import os def list_file(file_name): data = [] file_name = file_name with open(file_name,'r') as f: for line in f.readlines(): line = line.strip('\n') data.append(line) return data def list_dir(dir_name): data = [] dir_name = dir_name for root,dirs,files in os.walk(dir_name): #exclude.txt写入需要排除比较的目录 for rdir in open("./exclude.txt"): rdir = rdir.strip('\n') if rdir in dirs: dirs.remove(rdir) #print dirs for filename in files: filepath = os.path.join(root,filename) #f.write(filepath+'\n') data.append(filepath) return data if __name__ == "__main__": keep_file = 'file.txt' local_dir = '/opt/app/fileops/testdir/' keep_path = list_file(keep_file) local_path = list_dir(local_dir) for i in local_path: if i not in keep_path: #print os.path.getctime(i) #os.remove(i) print i

 

posted @ 2018-04-14 14:44  行知散人  阅读(653)  评论(0编辑  收藏  举报