我編寫了一個API,該API以目錄作為輸入,它將將該文件夾(及其子文件夾)中的每個文本文件(.txt)加載到Postgres DB中。該API適用于一些文件(大約3個),但是當它開始“讀取”第四個文件時,無論文件是什么,程序都會崩潰。我什至將文件分成三個部分,API仍然停止。我也得到一個 curl: (52) Empty reply from server這是代碼:@apiR2A.route('/api/lectura', method=['POST'])def read_txt(): #get_postgres_connection() arch = [] #list of succesfully read files arch_err = [] #list of files with errors con = create_connection() #creating connection with db archivos = request.query.archivos for root, dirs, files in os.walk(archivos): for file in files: #looping through all the files inside root if file.endswith(".txt"): #only looking for text files #creating dataframe df = pd.read_csv(os.path.join(root, file), encoding="utf-8", sep="|", header=None) if len(df.columns) is not 12: #verifying num of columns print("WARNING: File {} has wrong format\n".format(os.path.join(root, file))) arch_err.append(file) continue sleep(10) df.columns = ["1","2","3","4","5","6","7","8","9","10","11","12"] #setting column names print('Reading: {}'.format(os.path.join(root, file))) #adding df to db df.to_sql('FBDClientesCuentas', con, if_exists='append', index=False) print('{} succesfully added to db.\n'.format(file)) sleep(5) arch.append(file) if len(arch) > 0: print('Files loaded to database:') for x in arch: print(x) if len(arch_err) > 0: print('\nUnread files:') for x in arch_err: print(x)編輯:我忽略了似乎是問題的根源,API的運行方式如下:if __name__ == '__main__': apiR2A.run( server='tornado', host='0.0.0.0', port=3000, reloader=True)問題出在我選擇運行API的服務器上
- 1 回答
- 0 關注
- 323 瀏覽
添加回答
舉報
0/150
提交
取消