1

I have a small chunk of code using Pandas that reads an incoming CSV, performs some simple computations, adds a column, and then turns the dataframe into a CSV using to_csv.

I was running it all in a Jupyter notebook and it worked great, the output csv file would be there right in the directory when I ran it. I have now changed my code to be run from the command line, and when I run it, I don't see the output CSV files anywhere. The way that I did this was saving the file as a .py, saving it into a folder right on my desktop, and putting the incoming csv in the same folder.

From similar questions on stackoverflow I am gathering that right before I use to_csv at the end of my code I might need to add the path into that line as a variable, such as this.

path = 'C:\Users\ab\Desktop\conversion'

final2.to_csv(path, 'Combined Book.csv', index=False)

However after adding this, I am still not seeing this output CSV file in the directory anywhere after running my pretty simple .py code from the command line.

Does anyone have any guidance? Let me know what other information I could add for clarity. I don't think sample code of the pandas computations is necessary, it is as simple as adding a column with data based on one of my incoming columns.

6
  • @ Warthog1, what is the purpose of adding a path , does it referring to Combined Book.csv Commented Sep 26, 2018 at 15:47
  • How exactly do you run your script? Commented Sep 26, 2018 at 15:48
  • 2
    are you looking for something ` final2.to_csv('C:\Users\ab\Desktop\conversion\Combined Book.csv', index=False) Commented Sep 26, 2018 at 15:48
  • The path is the desired output folder, which is the folder that my .py and csv file are in. I want to add the path because right now the code doesn't output any CSV files, let alone into the desired folder. From research it looks like adding a path would remove that problem Commented Sep 26, 2018 at 15:49
  • @ w-m I am running it from the command line by calling convertbook.py and it executes without error Commented Sep 26, 2018 at 15:50

4 Answers 4

3

Join the path and the filename together and pass that to pd.to_csv:

import os
path = 'C:\Users\ab\Desktop\conversion'
output_file = os.path.join(path,'Combined Book.csv')

final2.to_csv(output_file, index=False)
Sign up to request clarification or add additional context in comments.

Comments

1

Im pretty sure that you have mixed up the arguments, as shown here. The path should include the filename in it.

path = 'C:\Users\ab\Desktop\conversion\Combined_Book.csv'

final2.to_csv(path, index=False)

Otherwise you are trying to overwrite the whole folder 'conversions' and add a complicated value separator.

Comments

1

I think below is what you are looking for , absolute path

import pandas as pd
.....
final2.to_csv('C:\Users\ab\Desktop\conversion\Combined Book.csv', index=False)

OR for an example:

path_to_file = "C:\Users\ab\Desktop\conversion\Combined Book.csv"
final2.to_csv(path_to_file, encoding="utf-8")

Comments

1

Though late answer but would be useful for someone facing similar issues. It is better to dynamically get the csv folder path instead of hardcoding it. We can do so using os.getcwd(). Later join the csv folder path with csv file name using os.path.join(os.getcwd(),'csvFileName')

Example:

import os
path = os.getcwd()
export_path = os.path.join(path,'Combined Book.csv')
final2.to_csv(export_path, index=False, header=True)

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.