Python:写完后无法删除文件

时间:2015-03-09 16:54:31

标签: python csv

我有一个大的管道分隔文件,我需要根据字段12中的week_number拆分成片段。我编写了以下脚本,看看每行是否匹配,是否将其写入另一个csv文件并使用gzip的。当我尝试删除未压缩的csv文件时出现问题,我收到此消息:

C:\data\weeks_files\week_01.csv
Traceback (most recent call last):
  File "C:\src\python\20150309_laptop_gbm_v1.py", line 45, in <module>
    os.remove(outFileName)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'C:\\data\\weeks_files\\week_01.csv'
[Finished in 0.2s with exit code 1]

以下是代码:

import csv
import re
import gzip
import os

input_file = "C:\\data\\20150226_train_mini.txt"

i_f = open( input_file, 'r' )
reader = csv.reader( i_f , delimiter = '|' )
# reader.next()

for i in range(101,153):

    trainWeek = i
    testWeek = i + 1

    trainPat =  str(trainWeek)[-2:] 
    testPat =  str(testWeek)[-2:] 
    print "trainPat: " , trainPat

    outFileName = "C:\\data\\weeks_files\\week_" + trainPat + ".csv"
    print outFileName

    outFile = open(outFileName, 'wb') 
    mywriter = csv.writer(outFile, delimiter = '|')

# for line in reader:
    for q in range(1,1000):
        line = next(reader)
    # print line
        # print "line[12]:" ,line[12]
        if trainPat in str(line[12]):
            # print "Success!"
            mywriter.writerow(line)
        else:
            line

    # import gzip
    f_in = open(outFileName, 'rb')
    f_out = gzip.open(outFileName+'.gz', 'wb')
    f_out.writelines(f_in)
    f_out.close()
    f_in.close()

    os.remove(outFileName)

有任何解决此问题的建议吗?

1 个答案:

答案 0 :(得分:3)

正如u_mulder暗示的那样,你必须在删除之前关闭文件。所以添加

outFile.close()

在你的

之前
# import gzip

评论