Unable to teardown temporary file used by Pandas read_csv

Viewed 379

My unittest teardown code does not remove the file

test_utility1.dat

I even tried os.remove and also no luck. I don't understand why my process is holding on to the file after raising an error. I am running this on python 3.6 and using Pycharm as my IDE. All my tests passed, except the teardown.

i mean i tried to mock, but i had no luck (newbie, and i just could not understand or grasp the concept). So i had to result to creating a temporary file and deleting it. I had the same problem, so i thought it will work better if i tried the textfixture library. but same error.

UPDATED MY POST

  • i have created a file directly on the os called test_utility1.dat
  • removed the old post which showed my temporary file creation
  • still the same result.

Please help. thank you

protoype7.py

import sys
import pandas

def create_utility_config_dataframe(a):

cols = ['Name', 'D', 'L', 'J', 'H', 'E', 'M', 'RF', 'AF']
try:
    return pandas.read_csv(a, sep='\t', usecols=cols)

except Exception as ve:
    ve = (str(ve) + "\n\nPlease ensure utility config '%s' exist and is correctly formatted (tab delimited)" %a)
    sys.exit(ve)

test2_prototype7.py

from prototype7 import create_utility_config_dataframe
import os
import unittest

class TestCreateUtilityConfigDataFrame(unittest.TestCase):

    @classmethod
    def tearDownClass(cls):
        os.remove('test_utility1.dat')

    def test_when_parameters_is_invalid(self):

        self.assertRaises(SystemExit, create_utility_config_dataframe,'test_utility1.dat')

errorlog

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'test_utility1.dat'

test_utility1.dat

Name.D.L.J.H.E.M.RF.AF
line1.150.4.2.150.2.Copper.1.true
1 Answers
Related