我正在编写一个名为OUT_FILE的csv文件。现在我可以看到该文件没有立即在磁盘上创建,所以我想等到文件被创建。
下面是编写csv文件的代码:
with open(OUT_FILE, 'a') as outputfile:
with open(INTER_FILE, 'rb') as feed:
writer = csv.writer(outputfile, delimiter=',', quotechar='"')
reader = csv.reader(feed, delimiter=',', quotechar='"')
for row in reader:
reportable_jurisdiction=row[7]
if '|' in reportable_jurisdiction:
row[7]="|".join(sorted(list(row[7].split('|'))))
print " reportable Jurisdiction with comma "+reportable_jurisdiction
else:
print "reportable Jurisdiction if single "+reportable_jurisdiction
writer.writerow(row)
feed.close()
outputfile.close()
现在我有一个名为FEED_FILE的文件,实际上是OUT_FILE的输入,即在OUT_FILE上输入数据后,OUT_FILE和FEED_FILE的大小应相同。
所以同样我写了下面的代码:
while True:
try:
print'sleeping for 5 seconds'
time.sleep(5)
outputfileSize=os.path.getsize(OUT_FILE)
if( outputfileSize ==FeedFileSize ):
break
except OSError:
print " file not created "
continue
print " file created !!"
现在我不知道这是否正在执行,因为没有错误甚至打印没有输出?
任何帮助?
答案 0 :(得分:0)
您可以使用python的 os 模块检查文件是否存在?
import os
def wait_for_file(path):
timeout = 300
while timeout:
if os.path.exists(path):
# check if your condition of file size having
# same as feed file size is met
return
timeout = timeout - 5
sleep(5)
raise Exception, "File was not created"