Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/00-Python Object and Data Structure Basics/08-Files.ipynb
Views: 648
Files
Python uses file objects to interact with external files on your computer. These file objects can be any sort of file you have on your computer, whether it be an audio file, a text file, emails, Excel documents, etc. Note: You will probably need to install certain libraries or modules to interact with those various file types, but they are easily available. (We will cover downloading modules later on in the course).
Python has a built-in open function that allows us to open and play with basic file types. First we will need a file though. We're going to use some IPython magic to create a text file!
IPython Writing a File
This function is specific to jupyter notebooks! Alternatively, quickly create a simple .txt file with sublime text editor.
Python Opening a file
Let's being by opening the file test.txt that is located in the same directory as this notebook. For now we will work with files located in the same directory as the notebook or .py script you are using.
It is very easy to get an error on this step:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-1-dafe28ee473f> in <module>()
----> 1 myfile = open('whoops.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'whoops.txt'
To avoid this error,make sure your .txt file is saved in the same location as your notebook, to check your notebook location, use pwd:
**Alternatively, to grab files from any location on your computer, simply pass in the entire file path. **
For Windows you need to use double \ so python doesn't treat the second \ as an escape character, a file path is in the form:
For MacOS and Linux you use slashes in the opposite direction:
This happens because you can imagine the reading "cursor" is at the end of the file after having read it. So there is nothing left to read. We can reset the "cursor" like this:
You can read a file line by line using the readlines method. Use caution with large files, since everything will be held in memory. We will learn how to iterate over large files later in the course.
When you have finished using a file, it is always good practice to close it.
Writing to a File
By default, the open()
function will only allow us to read the file. We need to pass the argument 'w'
to write over the file. For example:
Use caution!
Opening a file with 'w'
or 'w+'
truncates the original, meaning that anything that was in the original file is deleted!
Appending to a File
Passing the argument 'a'
opens the file and puts the pointer at the end, so anything written is appended. Like 'w+'
, 'a+'
lets us read and write to a file. If the file does not exist, one will be created.
Appending with %%writefile
We can do the same thing using IPython cell magic:
Add a blank space if you want the first line to begin on its own line, as Jupyter won't recognize escape sequences like \n
Iterating through a File
Lets get a quick preview of a for loop by iterating over a text file. First let's make a new text file with some IPython Magic:
Now we can use a little bit of flow to tell the program to for through every line of the file and do something:
Don't worry about fully understanding this yet, for loops are coming up soon. But we'll break down what we did above. We said that for every line in this text file, go ahead and print that line. It's important to note a few things here:
We could have called the "line" object anything (see example below).
By not calling
.read()
on the file, the whole text file was not stored in memory.Notice the indent on the second line for print. This whitespace is required in Python.
We'll learn a lot more about this later, but up next: Sets and Booleans!