0

I have read over the cases previously asking about why os.remove is getting

WindowsError: [Error 32] The process cannot access the file because it is being used by another process.

I've tried using the with open(csvPath,"r") as csvData, but when I do that I start getting the error:

TypeError: 'str' object does not support item assignment

If I comment out the with open and os.remove then the file will generate, but I still need to delete the source file. I'm not worried right now about the comment on line 13, as I can fix that later, but that I can't delete the source file is a bit of a problem.

import csv
import os
import datetime

for file in os.listdir(".\Pending"):
    if file.endswith('.csv'):
        csvFile = file
        csvPath = (os.path.join(".\Pending",csvFile))

        xmlFile = os.path.join('.\Processed',os.path.splitext(csvFile)[0] + '_' + datetime.datetime.today().strftime('%Y%m%d') + '.xml')

        csvData = csv.reader(open(csvPath))
        # Right now only comma delimitation is supported. This needs to be extended

        # Make sure it is possible to write to the destination folder
        try:
            xmlData = open(xmlFile, 'w')
            xmlData.write('<?xml version="1.0"?>' + "\n")
            # there must be only one top-level tag
            xmlData.write('<csv_data>' + "\n")

            rowNum = 0
            for row in csvData:
                if rowNum == 0:
                    tags = row
                    # replace spaces w/ underscores in tag names
                    for i in range(len(tags)):
                        tags[i] = tags[i].replace(' ', '_')
                else: 
                    xmlData.write('<row>' + "\n")
                    for i in range(len(tags)):
                        xmlData.write('    ' + '<' + tags[i] + '>' \
                                    + row[i] + '</' + tags[i] + '>' + "\n")
                    xmlData.write('</row>' + "\n")

                rowNum +=1

            xmlData.write('</csv_data>' + "\n")
            xmlData.close()

            # IF there are no errors in the transform, delete from the pending path
            # How do I catch unknown errors? What errors are possible within the transform?
            os.remove(csvPath)

        except IOError:
            errorFile = file
            errorPath = (os.path.join(".\Pending",errorFile))
            logFile = os.path.join('.\Error',os.path.splitext(errorFile)[0] + '_' + datetime.datetime.today().strftime('%Y%m%d') + '.txt')

            os.rename(os.path.join(".\Error",errorFile))
            os.remove(errorPath)

            log = open(logFile, 'w')
            log.write("Cannot write to the Processed folder")
            log.close()

    else:
        errorFile = file
        errorPath = (os.path.join(".\Pending",errorFile))
        logFile = os.path.join('.\Error',os.path.splitext(errorFile)[0] + '_' + datetime.datetime.today().strftime('%Y%m%d') + '.txt')

        os.rename(os.path.join(".\Error",errorFile))
        os.remove(errorPath)

        log = open(logFile, 'w')
        log.write("File is not a CSV extension")
        log.close()
Tim
  • 3,178
  • 1
  • 13
  • 26
Jason H
  • 168
  • 2
  • 12

1 Answers1

1

It's always good practice to use

with open(filename, 'r') as f:
    dostuff...

because the file automatically closes after exiting the "with" statement.


Another solution involves "pandas" package. "pandas" has a "read_csv" method that closes the csv automatically after reading if you supply the name of the csv file. For example,

import pandas

data = pandas.read_csv(filename)
# file is already closed here
Tim
  • 3,178
  • 1
  • 13
  • 26