Home > Software engineering >  How do change the save location of files when file names are being saved based on a list?
How do change the save location of files when file names are being saved based on a list?

Time:03-13

How can I customize the save path for the to_csv function below? I have multiple files that are being pulled via Python and the files are being saved as with the date being substituted into the {date} below, but it's saving to where the script is located. How can I change the save location?

df_call.to_csv(f'call_{date}.csv')
df_put.to_csv(f'put_{date}.csv')

CodePudding user response:

You can write path;

windows

df_call.to_csv(f'C:\\Users\\YOURUSERNAME\\Desktop\\call_{date}.csv')

linux

df_put.to_csv(f'~/Desktop/put_{date}.csv')

CodePudding user response:

Have a look at path and pathlib modules in the python standard library. Utilizing these modules, your operations will be more robust and work across different OS.

To save a file to a parent folder, you can do the following:

from pathlib import Path
...
scriptpath = Path(__file__) # e.g. /home/usr/scripts/script.py
parentfolder = scriptpath.parents[1] # /home/usr/
outputdir = parentfolder / 'output' # /home/usr/output
filename = 'output.csv'
outputpath = outputdir / filename /home/usr/output/output.csv
  • Related