我陷入了一些我認爲應該足夠簡單的事情。 我正在創建一個包含json字符串的文件,以導入到postgres數據庫中。但是,即使python腳本的內部測試顯示它存在,該文件也不會導入。直到腳本完成後文件才寫完
但是,如果我在腳本完成後執行postgres導入,它將複製正常,或者如果我將它們包裝在單獨的腳本中並從單個腳本中調用它,它將起作用,但如果兩個請求都處於相同腳本。我試過close(),fsync和flush但沒有運氣..
任何人都可以幫忙嗎?
相關代碼如下。
command=str("PGPASSWORD=password psql -d database -U postgres -V -c \"copy import.table from Data.txt'\"")
print command
dataFile=open('Data.txt','w')
for x in xx:
xString=json.loads(data)
xString[i]['source']=x
xString[i]['created_at'].replace('"','')
xStringJson=json.dumps(xString)
dataFile.write(xStringJson)
dataFile.close
dataFile.flush()
os.fsync(dataFile)
print os.path.isfile('Data.txt')
pg_command(command)
i=i+1
'f.close'不是'f.close()' –