cannot write file with full path in Python

前端 未结 2 488
深忆病人
深忆病人 2021-01-07 07:22

I am using Pandas on Mac, to read and write a CSV file, and the weird thing is when using full path, it has error and when using just a file name, it works. I post my code w

相关标签:
2条回答
  • 2021-01-07 08:13

    Try using os.path.join().

    import os
    (...)
    output_filename = 'newMaster.csv'
    output_path = os.path.join('Downloads', output_filename)
    (...)
    sourceDf.to_csv(output_path)
    

    Use the same methodology to point pandas.read_csv() in the right direction.

    0 讨论(0)
  • 2021-01-07 08:14

    You didn't specify python version. On 3.4 you can use pathlib, otherwise use os.path.join() or quoting:

    sourceDf.to_csv(r'~/Downloads/newMaster.csv')
    

    Notice the r. The problem is that /n is newline, which is not allowed in a path.

    0 讨论(0)
提交回复
热议问题