from datetime import datetime def Main(): source_dir = 'E:/code/Modbus_RTU/pass.txt' target_dir = 'E:/code/Modbus_RTU/passs/' # 計數器 flag = 0 # 文件名 name = 1 # 存放數據 dataList = [] print("開始。。。。。") print(datetime.now().strftime('%Y-%m-%d %H:%M:%S')) with open(source_dir, 'r') as f_source: for line in f_source: flag += 1 dataList.append(line) if flag == 100000: with open(target_dir + "pass_" + str(name) + ".txt", 'w+') as f_target: for data in dataList: f_target.write(data) name += 1 flag = 0 dataList = [] # 處理最後一批行數少於200萬行的 with open(target_dir + "pass_" + str(name) + ".txt", 'w+') as f_target: for data in dataList: f_target.write(data) print("完成。。。。。") print(datetime.now().strftime('%Y-%m-%d %H:%M:%S')) if __name__ == "__main__": Main()