I have a Python 3 test.py
script where some data is created which I want to save to a file with a user-defined name, say data.csv
. The folder in which this script is sitting has a /Results
folder inside it. What I want to do is to create a subfolder inside Results
with the name of the current script and store my file inside it. So, what I want is my file to be stored in ./Results/test/data.csv
. I want these folders to be created if they don't previously exist and if they do exist, along with the file, I want data.csv
to be replaced. I want this to work on any OS. How should this be done "pythonically"?
~/
test.py
Results/
test/
data.csv
Here's a test code where data.csv
is not being saved where I want it. How should I edit it?
import pandas as pd
filename = "data.csv"
df = pd.DataFrame([1,2,3])
df.to_csv(filename)