循环,直到在python中创建文件

循环,直到在python中创建文件,python,csv,Python,Csv,我正在写一个名为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(fee

我正在写一个名为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_文件的输入,即在OUT_文件上写入数据后,OUT_文件和FEED_文件的大小应该相同

为此,我编写了以下代码:

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 !!"
现在我不知道这是否正在执行,因为没有错误,甚至输出中也没有打印


有什么帮助吗?

您可以使用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"

您是否试图以“并行”(线程化或多进程设置)或在退出
输出文件的
with
子句后,
循环执行这些操作?ioerror与oserror不同。@zwer。。我的while循环在exit to with子句之后。。代码与上面提到的完全相同。此处未更改任何内容。已达到超时并引发异常“文件未创建”。它缺少睡眠。但这只是一个想法。显然你不能直接把它拔出来!