0

I need to export my pandas dataframe as a csv in to the folder. My python solution is deployed on the IIS Server, where i get the "file_path" and the url, i use these two things to fetch or read my file. This is what i did to read the incoming file.

from urllib.parse import quote

url = "http://20.9.6.11:8066" -- given
file_path = "file_upload/file_info/products/class1/file.csv" --given
incoming_file_path = url + "/" + quote(file_path)
df = pd.read_csv(incoming_file_path)

i am able to fetch or read my csv file successfully with the above code,But after my data get processed i need to export that csv to some other folder, that i am not able to export. I did:

folder_to_export_path = "http://20.9.6.11:8066/file_info/products/processed_file"
clean_df.to_csv(r'folder_to_export_path+'filename.csv') # error
K RAJA
  • 19
  • 6

4 Answers4

0

Try this:

folder_to_export_path = "http://20.9.6.11:8066/file_info/products/processed_file/"
clean_df.to_csv(folder_to_export_path+'filename.csv')

Reference: here

CK__
  • 1,252
  • 1
  • 11
  • 25
0

have you tried

clean_df.to_csv(folder_to_export_path+'/filename.csv')

or if using python 3.6+

clean_df.to_csv(f'{folder_to_export_path}/filename.csv')
Jimmar
  • 4,194
  • 2
  • 28
  • 43
  • i tried the following error came : OSError: [Errno 22] Invalid argument: 'http://20.9.6.11:8066/file_info/products/processed_file/filename.csv' – K RAJA Aug 23 '20 at 11:40
  • @KRAJA is your code writing on the same server it's running from ? – Jimmar Aug 23 '20 at 12:12
  • Yes it's on the same server. – K RAJA Aug 23 '20 at 17:17
  • @KRAJA then remove the `url` part, you can write to the path directly like `clean_df.to_csv("file_info/products/processed_file/filename.csv")` – Jimmar Aug 23 '20 at 23:05
  • i tried your way FileNotFoundError: [Errno 2] No such file or directory: this error is coming. But when i tried clean_df.to_csv(" path_to_export") csv file has exported in my working directory with the name of "path_to_export" – K RAJA Aug 24 '20 at 05:54
  • @KRAJA yes that's because you need to go on your server and create the directories, make sure that `file_info/products/processed_file` exist in the server. – Jimmar Aug 24 '20 at 07:39
  • the file location should also access when i hit my API from outside server. For that you need to use the full url location. – K RAJA Aug 24 '20 at 18:19
  • @KRAJA yes, only do that for the writing part not the reading part – Jimmar Aug 24 '20 at 18:37
0

Your last line has syntax error , try to correct it with the following :

clean_df.to_csv(folder_to_export_path+'/filename.csv')
Sowjanya R Bhat
  • 1,128
  • 10
  • 19
  • i tried your code following error came OSError: [Errno 22] Invalid argument: 'http://20.9.6.11:8066/file_info/products/processed_file/filename.csv' – K RAJA Aug 23 '20 at 11:43
0

First of all, with to_csv you need to pass a file path (or a file object). So that makes your folder_to_export_path='http://20.9.6.11:8066/file_info/products/processed_file' invalid.

It should be folder_to_export_path = 'file_info/products/processed_file/filename.csv' given that you want to export the CSV in file_info/products/processed_file directory.

Secondly, to_csv can create a file named filename.csv if it doesn't exist but it cannot create directories.

To create directories if they don't exist and then save a file you can do:

import os
import pandas as pd

folder_to_export_path = 'file_info/products/processed_file/'
if not os.path.exists(folder_to_export_path):
    os.makedirs(folder_to_export_path)

pd.to_csv(os.path.join(folder_to_export_path, 'filename.csv'))

That should work.

Omkar Manjrekar
  • 103
  • 1
  • 11
  • i tried your way, when i ran my code from my local, it created folder in my working directory which is somewhere else. And why not my folder_to_export_path = 'http://20.9.6.11:8066/file_info/products/processed_file/' because this is my actual file location where i want to export my df. It should access the file location when i hit my api from outside server. – K RAJA Aug 24 '20 at 18:17
  • Then specify absolute path of the directory like so `/home/kraja/file_info/products/processed_file/`. – Omkar Manjrekar Aug 24 '20 at 19:09