Search code examples
python-3.xpandasopencsv

python open() vs pd.read_csv()


a photo of using open() and pd.read_csv() to open a link but open() says no file directory, why do I have an error when I used open() but pd.read_csv opened the file, I tried using it again but it says file not found?


Solution

  • The function open() opens a file which resides in persistent memory (SSD/HDD), and returns a file object.

    pandas.read_csv() can parse input directly from URLs.

    In other words: Because the file, as the error says, does not exist and neither does a path to said file.

    Edit: I just realized that you may be new here, so maybe I'll give an analogy: Imagine I told you to open a book, and said out loud a link to a book. You would wonder how you would ever open that book as well, because you have no way of parsing that information in your head. If I instead send a you link, which you open on your computer, you can read the file. open() wants a physical book. pd.read_csv() can also accept a link to the book.