简体   繁体   English

Python-to_csv-找不到目录/ iPython笔记本可以

[英]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\\'. 我编写了一个小的Python脚本来提取数据并将其与熊猫一起以CSV文件的形式保存在共享驱动器“ 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: 当我在iPython Notebook中运行代码时,它运行良好,但如果通过命令行运行相同的代码,则会崩溃并显示:

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? 我正在Windows上运行它。有人知道如何解决该问题吗?

Thanks in advance! 提前致谢!

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. 我设法通过cmd.exe运行它,我认为我使用的unix bash模拟器无法识别此N:/驱动器。

Thanks! 谢谢!

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: 因此,我建议先尝试将路径提供给os.path.abspath ,该路径应注意双斜杠,然后使用df.to_csv:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM