Section 8.10 Summary
You can open files for reading or writing which returns a file object, close a file object, and read from file objects. The file object is iterable which means that you can loop though all the lines in a file using a for each loop.
Method | Example | Description |
---|---|---|
open | file_obj = open(filename) | Opens the file named filename for reading and returns a file object |
open | file_obj = open(filename, “r”) | Opens the file named filename for reading and returns a file object |
open | file_obj = open(filename, ‘w') | Opens the file named filename for writing and returns a file object |
close | file_obj.close() | Closes an open file (most crucial when writing to a file) |
read | data = file_obj.read() | Reads all data from a file object and returns it as a string |
readline | line = file_obj.readline() | Returns a line from a file object as a string |
readlines | lines = file_obj.readlines() | Returns a list of lines from a file object |
write | file_obj.write(line) | Writes the line to the file object |
When reading from files you may need to change the current directory to the one with the file to read from. These are some of the useful terminal commands for navigating the directory structure.
Mac | Windows | Description |
---|---|---|
cd | cd | Change directory - takes you to the root directory |
cd dir | cd dir | Will change the current directory to the specified directory |
cd .. | cd.. | Change directory up one level (move to the parent directory) |
pwd | chdir | Print working directory (pwd) or show the current directory |
ls | dir | List the contents of the current directory |
When working with git and GitHub these are some of the useful commands.
Command | Description |
---|---|
git clone URL | Download a copy of the repo at the given URL to the current directory |
git add file | Add the specified file to the list of files to be committed |
git status | What files have been added and/or changed |
git commit -m “message” | Package changes into a commit on your local computer |
git push | Push commits to your GitHub account |
git pull | Download changes from GitHub to your local repo |