1

I have .json file for paths and i have tried to write csv as below code :

#.json file data
"source_and_destination_details": {
  "local_uri_write": "/home/zmo-ubt-preetim-01/Desktop/",
  "folder_name_for_unmatched_column_data": "unmatched_column_data",
  "file_name_for_unmatched_column_data": "demo.csv",
},

# Writing csv on local
df.reindex(idx).to_csv(
    obj['source_and_destination_details']['local_uri_write'] + obj['source_and_destination_details'][
        'folder_name_for_unmatched_column_data'] + obj['source_and_destination_details'][
        'file_name_for_unmatched_column_data'],
    index=False)

Traceback (most recent call last):
  File "/home/zmo-ubt-preetim-01/PycharmProjects/pythonProject/main.py", line 48, in <module>
    index=False)
  File "/home/zmo-ubt-preetim-01/PycharmProjects/pythonProject/venv/lib/python3.6/site-packages/pandas/core/generic.py", line 3170, in to_csv
    formatter.save()
  File "/home/zmo-ubt-preetim-01/PycharmProjects/pythonProject/venv/lib/python3.6/site-packages/pandas/io/formats/csvs.py", line 190, in save
    compression=dict(self.compression_args, method=self.compression),
  File "/home/zmo-ubt-preetim-01/PycharmProjects/pythonProject/venv/lib/python3.6/site-packages/pandas/io/common.py", line 493, in get_handle
    f = open(path_or_buf, mode, encoding=encoding, errors=errors, newline="")
FileNotFoundError: [Errno 2] No such file or directory: '/home/zmo-ubt-preetim-01/Desktop/unmatched_column_data/demo.csv'

I am getting the above error

I want to store demo.csv file at in loaction in new folder named

unmatched_column_data
Priti M
  • 17
  • 6

1 Answers1

0

to_csv() does not create a new folder. You can see it on python documentation

To write a csv file to a new folder or nested folder you will first need to create it using either

Following code can be used as you can also see in python documentation link above:

import os  
os.makedirs('folder/subfolder', exist_ok=True)  
df.to_csv('folder/subfolder/out.csv')  

Hope it helps.

ozlemg
  • 436
  • 2
  • 10
  • great thank you..... but can we use this to create folder in AWS s3 via jyputer notebook? – Priti M Dec 29 '22 at 11:19
  • This question may answer your question : https://stackoverflow.com/questions/34191729/create-directories-in-amazon-s3-using-python-boto3 – ozlemg Dec 29 '22 at 12:14
  • yes It did but how can I store the csv file into that created folder in s3? – Priti M Jan 02 '23 at 06:58
  • https://stackoverflow.com/questions/38154040/save-dataframe-to-csv-directly-to-s3-python may be the answer to save a csv file to s3. – ozlemg Jan 09 '23 at 08:02