'How to save data frame into specific folder based on System date in Python
Currently I am working on one directory and my Python file(.ipynb) file is stored in the same directory.
Directory currently I am working = "C:\Users\XYZ\Desktop\ABC\Data Science"
After working on a data set I am dumping my data frame into new folder = "C:\Users\XYZ\Desktop\ABC\Data Science\Dataset".
I have a requirement where I have to save the data based on Date and time.
I have used below method
TodaysDate = time.strftime("%d-%m-%Y-%H-%M-%S")
excelfilename_pass = TodaysDate +".csv"
passed_data.to_csv(excelfilename_pass)
But the above code is getting saved in "C:\Users\XYZ\Desktop\ABC\Data Science" this folder whereas I want the data frame to be saved in "C:\Users\XYZ\Desktop\ABC\Data Science\Dataset" folder.
Any help would be appreciated.
Solution 1:[1]
Try using the full path
folder_path = r"C:\Users\XYZ\Desktop\ABC\Data Science\Dataset"
file_path = os.path.join(folder_path, "{}.csv".format(time.strftime("%d-%m-%Y-%H-%M-%S")))
passed_data.to_csv(file_path )
Solution 2:[2]
If your code is currently in the Data Science folder, I would simply add the dataset to your file path.
So excelfilename_pass = "dataset/" + TodaysDate + ".csv"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CN-LanBao |
| Solution 2 | jinx |
