python基礎:讀寫檔案和合並檔案
阿新 • • 發佈:2019-02-09
這是一個讀寫檔案和合並檔案的範例
讀入了基礎詞庫的路徑based_file_path和新增詞庫的路徑added_file_path,將詞庫合併去重後寫入merge_file_path路徑下的檔案
def merge_dict_func(based_file_path, added_file_path, merge_file_path): based_file_object = open(based_file_path, 'r') added_file_object = open(added_file_path, 'r') merge_file_object = open(merge_file_path, 'w') based_set = [] added_set = [] merge_set = [] for line in based_file_object: based_set.append(line) for line in added_file_object: added_set.append(line) for added_x in added_set: if added_x not in based_set: merge_set.append(added_x) for based_x in based_set: merge_set.append(based_x) for merge_x in merge_set: if merge_x != None: merge_file_object.write(merge_x) merge_file_object.close() based_file_object.close() added_file_object.close() if __name__ == '__main__': if based_file_path != None and added_file_path != None and merge_file_path != None: merge_dict_func(based_file_path, added_file_path, merge_file_path) print 'merge_dict_util process finished.'</span>