# Jupyter Basics: Reading Files

Read Files From Local Storage Into a Jupyter Notebook Using the File Path

![nj-jupyter-sm.png][nextjournal#file#d3a53c1f-7f18-4dbb-a686-944336ce3354] Loading data into a Python or a Python library is a common first step for people new to Jupyter notebooks. This is an introduction. # Upload Data to Nextjournal [artist_data.csv][nextjournal#file#245f63e3-9c8b-4bb4-8c66-f36c5fa9a411] Where did it go? Use `!ls` [magic command](https://ipython.readthedocs.io/en/stable/interactive/magics.html) to see. ```python id=5e8addf7-9263-494a-b08f-8e4babaaa7dc !ls [reference][nextjournal#reference#eb66e58a-6a09-4ced-8572-3b9009499c37] ``` # Move Data to a Project Directory Make sure you collect all your data in a logical place when creating a Jupyter project. Use the `%%bash` magic to use command line operations within a Jupyter cell. ```python id=15ada336-e9c8-4eb9-87c1-2feef00ef08a %%bash mkdir -p /project/artist/ cp /.nextjournal/data-named/QmaVnxKK4DkqiVEG5Puh5bEZxwh7yxsfksSHUPEmUbAXpV/artist_data.csv /project/artist/artist_data.csv ls /project/artist ``` # Load Into Memory ## Opening and Closing the File There are [several switches](https://docs.python.org/3/library/functions.html#open) available to the `open()` method. For example: * `r`: read mode * `w`: write mode * `a`: append mode ```python id=24276a99-bfd3-4299-8279-a9a275138f7a f = open("/project/artist/artist_data.csv","r") print(f.read()) f.close() ``` Don't forget to `close()` the file after opening it. ## Opening a File With a Library ```python id=d113839b-601d-4b66-9e94-8a0c26198903 import pandas as pd data = pd.read_csv("/project/artist/artist_data.csv") data.head(6) ``` [result][nextjournal#output#d113839b-601d-4b66-9e94-8a0c26198903#result] [nextjournal#file#d3a53c1f-7f18-4dbb-a686-944336ce3354]: [nextjournal#file#245f63e3-9c8b-4bb4-8c66-f36c5fa9a411]: [nextjournal#reference#eb66e58a-6a09-4ced-8572-3b9009499c37]: <#nextjournal#reference#eb66e58a-6a09-4ced-8572-3b9009499c37> [nextjournal#output#d113839b-601d-4b66-9e94-8a0c26198903#result]:
This notebook was exported from https://nextjournal.com/a/Lfz2SH9KNGUrbHniSz23N?change-id=CYuYvj89dsUN4Ap5b5iu8u ```edn nextjournal-metadata {:article {:settings {:subtitle? true}, :nodes {"15ada336-e9c8-4eb9-87c1-2feef00ef08a" {:compute-ref #uuid "bf96b304-7315-432e-b8c9-ff6891b4d579", :exec-duration 417, :id "15ada336-e9c8-4eb9-87c1-2feef00ef08a", :kind "code", :output-log-lines {:stdout 1}, :runtime [:runtime "9d467892-cded-4afb-b354-64e7eb65e7bc"]}, "24276a99-bfd3-4299-8279-a9a275138f7a" {:compute-ref #uuid "51d0cc37-e981-420e-9bd7-39f391b0c1b4", :exec-duration 729, :id "24276a99-bfd3-4299-8279-a9a275138f7a", :kind "code", :output-log-lines {:stdout 3534}, :runtime [:runtime "9d467892-cded-4afb-b354-64e7eb65e7bc"]}, "245f63e3-9c8b-4bb4-8c66-f36c5fa9a411" {:id "245f63e3-9c8b-4bb4-8c66-f36c5fa9a411", :kind "file"}, "5e8addf7-9263-494a-b08f-8e4babaaa7dc" {:compute-ref #uuid "f84efa7d-7def-49d5-b7d9-b02e5fd93d85", :exec-duration 1507, :id "5e8addf7-9263-494a-b08f-8e4babaaa7dc", :kind "code", :output-log-lines {:stdout 1}, :runtime [:runtime "9d467892-cded-4afb-b354-64e7eb65e7bc"]}, "9d467892-cded-4afb-b354-64e7eb65e7bc" {:environment [:environment {:article/nextjournal.id #uuid "5b45e08b-5b96-413e-84ed-f03b5b65bd66", :change/nextjournal.id #uuid "5d5e6f27-0d15-4cb1-9781-c46e6505a2ec", :node/id "0149f12a-08de-4f3d-9fd3-4b7a665e8624"}], :id "9d467892-cded-4afb-b354-64e7eb65e7bc", :kind "runtime", :language "python", :type :jupyter}, "d113839b-601d-4b66-9e94-8a0c26198903" {:compute-ref #uuid "36726472-7c16-4018-a17e-a326ec85eb9b", :exec-duration 1238, :id "d113839b-601d-4b66-9e94-8a0c26198903", :kind "code", :output-log-lines {}, :runtime [:runtime "9d467892-cded-4afb-b354-64e7eb65e7bc"]}, "d3a53c1f-7f18-4dbb-a686-944336ce3354" {:id "d3a53c1f-7f18-4dbb-a686-944336ce3354", :kind "file"}, "eb66e58a-6a09-4ced-8572-3b9009499c37" {:id "eb66e58a-6a09-4ced-8572-3b9009499c37", :kind "reference", :link [:output "245f63e3-9c8b-4bb4-8c66-f36c5fa9a411" nil]}}, :nextjournal/id #uuid "02bf27e7-33e5-4972-9602-f05f8eb68f05", :article/change {:nextjournal/id #uuid "5d88f7ad-ecf7-4920-89a0-880a6bbe5a82"}}} ```