0

I need to dynamically save my pandas data frame. I have successfully managed to output a CSV file with a static name using the following code:

export_csv = df.to_csv(r'static_name.csv', header=False , index=False)

But I have failed to make this work dynamically. With the code below, I expect to get a file with the name passed into args.save_file_name and .csv suffix. However, I get no result.

import os
import argparse
import pandas as pd

parser = argparse.ArgumentParser()
parser.add_argument('file', help="this is the file you want to open")
parser.add_argument('save_file_name', help="the name of the file you want for the output CSV")

args = parser.parse_args()
print("file name:", args.file) # checking that this worked

...

# export csv
path = os.getcwd()
export_path = path + args.save_file_name + '.csv'
export_csv = df.to_csv(path_or_buf=export_path, header=False, index=False)
2
  • probably the file is created in this location os.getcwd() try printing the path and check if file exists Commented Aug 23, 2019 at 11:18
  • @MorrisGevirtz, please, check the answer, and do not forget to accept it in case it is helpful, or add an comment, if it is not. Commented Aug 30, 2019 at 8:25

1 Answer 1

1

I think, that problem is in your export_path variable, that is not made right. The following code should do the job.

export_path = os.path.join(path, args.save_file_name + '.csv')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.