1

I have this simple code which saves a pandas dataframe to a csv file. As of now, it works by overwriting the filename so each time I run it it just replaces the old file with a new one with the same name. Is it possible to save this dataframe but have it create new files sequentially, ie if there is already some file called "filename1" in the directory make the new one called "filename2" so the data from the original file is not just lost?

import pandas
datamatrix= [[1,2,3],[1,2,3],[1,2,3]])
x=pandas.DataFrame(datamatrix)  
pandas.DataFrame.to_csv(x,"filename.csv",',')
notAI
  • 111
  • 1
  • 10
  • 1
    Have a look at this question asked here already https://stackoverflow.com/questions/13852700/python-create-file-but-if-name-exists-add-number – Raja Sattiraju Jun 15 '18 at 20:07
  • This is not a pandas specific question. You want to read the files in folder before save a new one – rpanai Jun 15 '18 at 20:08
  • 1
    Possible duplicate of [python: Create file but if name exists add number](https://stackoverflow.com/questions/13852700/python-create-file-but-if-name-exists-add-number) – rpanai Jun 15 '18 at 20:09
  • You can explicitly check if a file exists before writing by using `os.path.exists(filename)`, but enumerating it into infinity may not really what you want, it may be a better idea to add a timestamp in one way or another to it that conflicts only once. Something like `filename_201806150311.csv` – d parolin Jun 15 '18 at 20:12

3 Answers3

2

I would probably save it with a different timestamp, unless there is a specific reason to have numerical numbering.

Using time stamp

import pandas
import time
datamatrix= [[1,2,3],[1,2,3],[1,2,3]]
x=pandas.DataFrame(datamatrix)  
pandas.DataFrame.to_csv(x,"filename_" + time.strftime('%Y-%m-%d %H-%S') + ".csv",',')

You could also just do a unix time stamp

pandas.DataFrame.to_csv(x,"filename_" + str(int(time.time())) + ".csv",',')

How to get current time in python and break up into year, month, day, hour, minute?

What is the easiest way to get current GMT time in Unix timestamp format?

RK1
  • 2,384
  • 1
  • 19
  • 36
0

As some of the comments have mentioned, this is not a question that has a pandas-specific answer. Simply use os.listdir(os.getcwd()) to list all of the files in your current working directory that you want to save the file to. If the filename that you are trying to save as already exists in the list returned from the above command, then increment your version number by 1, or save as a different filename entirely, up to you.

rahlf23
  • 8,869
  • 4
  • 24
  • 54
0

by using time.strftime and combinding f-string and raw string literals you can attach timestamp to your desired format

import pandas
import time
timestr = time.strftime("%Y%m%d-%H%M%S")
datamatrix= [[1,2,3],[1,2,3],[1,2,3]]
x=pandas.DataFrame(datamatrix)  
pandas.DataFrame.to_csv(fr'D:\filename_{timestr}.csv',encoding="utf-8", index=False, header=True)