2016-09-29 1 views
0

私は何かに悩まされています。 私はjson文字列を含むファイルを作成して、postgresデータベースにインポートします。しかし、pythonスクリプトによる内部テストではファイルが存在していても、ファイルはインポートされません。スクリプトが完了するまでファイルが書き込まれません

しかし、スクリプトが完了した後でpostgresのインポートを実行すると、問題なくコピーされます。または、別のスクリプトでラップして1つのスクリプトから呼び出すと、動作しますが、両方の要求が同じスクリプト。私は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 
+1

は 'f.close'は' f.close() ' –

答えて

4

ファイルを閉じていません。

これは何もしません、それは括弧が欠落していbecuase:

dataFile.close 

しかし、イベント、それは近くなかった場合、それはXXを介して第1の反復でそれを行うだろう。

は、このようにそれを実行します。

with open('Data.txt','w') as dataFile: 
    for x in xx: 
     # write to the file 

# when you are back here, the file is flushed, closed, and ready to be read. 
+0

おかげで、私はそれに気付かなかったし、それが例外をスローしないではありません。提案にも感謝します。 – Mokadillion

関連する問題