Ad

Exporting A Dataframe To CSV To The Same Directory As The Exporter Script

- 1 answer

Pardon if I couldn't synthesize precisely my problem with my title, however I guess that by explaning it, things will get more clear.

My problem is the following: I am sending a file containing two scripts to a client, one of them generates a set of articial data, in the form of a dataframe, and the other to import and process that data. So, I want my exported csv to be on the same folder as the script that will generate it, however I don't want to write the usual path manually, as I would write on my PC, because I am sending the file to someone else.

df.to_csv(r'Path where you want to store the exported CSV file\File_Name.csv')

So, lets say that the scripts are in a folder named "A", is there somehow to setting the generated CSV to be saved on this specific folder, a parent folder, as well as being imported by the second script, without having to write the whole folder path that I would normally do if I were on my PC?

I expect something like this:

df.to_csv(r'...\A\File_Name.csv')
Ad

Answer

You can use dot as relative path. ./ means current directory, ../ means parent directory.

df.to_csv('../File_Name.csv') means save the csv file to parent directory. df.to_csv('./A/File_Name.csv') means save the csv file to subfolder A.

By the way, dot relative path and the other answer os.getcwd(), work with where the script runing, not where the script exists.

like this python script/data.py, ./ means the directory of script directory, not the directory of data.py, same as os

Ad
source: stackoverflow.com
Ad