Pierre
Pierre

Reputation: 988

Python - to_csv - cannot find directory /iPython notebook can

I have written a small Python script to extract data and save it with pandas as a CSV file in a folder in a shared drive called 'N:\folder\'. The function used is:

df.to_csv('N:\MMS Managers\house_opportunities.csv', index=False, encoding='utf-8')

It works well when I run my code in iPython notebook but if I run exactly the same via the command line it crashes and says:

IOError: [Errno 2] No such file or directory: 'N:\\MMS Managers\\house_opportunities.csv'

I am running it on Windows Does anyone know how to fix that please?

Thanks in advance!

Upvotes: 0

Views: 579

Answers (2)

Pierre
Pierre

Reputation: 988

I managed to make it work running it via cmd.exe, I think the unix bash emulator I was using didn't recognize this N:/ drive.

Thanks!

Upvotes: 1

lev
lev

Reputation: 2977

The problem seems to be related to the double slashes you see in the error:

'N:\\MMS Managers\\house_opportunities.csv'

Thus, I would suggest trying first to give the path to os.path.abspath, which should take care of the double slashes, and then use df.to_csv:

import os
path = os.path.abspath('N:\MMS Managers\house_opportunities.csv')
df.to_csv(path, index=False, encoding='utf-8')

Upvotes: 0

Related Questions