1

I have a dataframe df and I want to write it into a csv file and load back:

import pandas as pd
import tempfile
with tempfile.NamedTemporaryFile() as temp:
    df.to_csv(temp.name + '.csv')
    data = pd.read_csv(temp.name + '.csv')

For some reason I do see the tempfile in my tmp/ directory. Please advise what should I do to eliminate it when I finish with the with scope?

SteveS
  • 3,789
  • 5
  • 30
  • 64
  • Not sure if this is relevant to your OS, sharing anyway: "Under Unix, the directory entry for the file is either not created at all or is removed immediately after the file is created. Other platforms do not support this; your code should not rely on a temporary file created using this function having or not having a visible name in the file system." from: https://docs.python.org/3/library/tempfile.html – mechanical_meat Dec 16 '21 at 15:42
  • If the above is relevant you could use `os.remove(temp.name + '.csv')` most likely, right? Reference: https://docs.python.org/3/library/os.html#os.remove – mechanical_meat Dec 16 '21 at 15:45
  • 1
    At least on Windows, the *temporary* file generated from `NamedTemporaryFile` is being deleted. A second file is being generated from `df.to_csv` in your temp directory that is not being automatically removed – Wondercricket Dec 16 '21 at 15:45
  • `tempfile.NamedTemporaryFile` has a named parameter `suffix` – perhaps you should add the suffix `csv` that way. – kirjosieppo Dec 16 '21 at 15:47
  • Ah, Wondercricket's correct. I believe I found a duplicate, or close enough other question: https://stackoverflow.com/a/43283261/42346 – mechanical_meat Dec 16 '21 at 15:48

0 Answers0