Python 3home |
Introduction to Python
davidbpython.com
Opening an existing file for writing truncates the file.
fh = open('new_file.txt', 'w')
fh.write("here's a line of text\n")
fh.write('I add the newlines explicitly if I want to write to the file\n')
fh.close()
Appending is usually used for log files.
fh = open('new_file.txt', 'w')
fh.write("here's a line of text\n")
fh.write('I add the newlines explicitly if I want to write to the file\n')
fh.close()
Again, note that we are explicitly adding newlines to the end of each line.
The pwd is the location from which we run our programs.
import os
cwd = os.getcwd() # str (your current directory)
print(cwd)
this tree can be found among your course files.
dir1 ├── file1.txt ├── test1.py │ ├── dir2a │ ├── file2a.txt │ ├── test2a.py │ │ │ ├── dir3a │ │ ├── file3a.txt │ │ ├── test3a.py │ │ │ │ │ └── dir4 │ │ ├── file4.txt │ │ └── test4.py └── dir2b ├── file2b.txt ├── test2b.py │ └── dir3b ├── file3b.txt └── test3b.py
These paths locate files relative to the present working directory.
If the file you want to open is in the same directory as the script you're executing, use the filename alone:
fh = open('filename.txt')
To reach the parent directory, prepend the filename with ../
:
fh = open('../filename.txt')
To reach the child directory, prepend the filename with the name of the child directory.
fh = open('childdir/filename.txt')
To reach a sibling directory, prepend the filename with ../ and the name of the child directory.
fh = open('childdir/filename.txt')
To reach a sibling directory, we must go "up, then down" by using ../ to go to the parent, then the sibling directory name to go down to the child.
These paths locate files from the root of the filesystem.
In Windows, absolute paths begin with a drive letter, usually C:\:
""" test3a.py: open and read a file """
filepath = r'C:\Users\david\Desktop\python_data\dir1\file1.txt'
fh = open(filepath)
print(fh.read())
(Note that r'' should be used with any Windows paths that contain backslashes.)
On the Mac, absolute paths begin with a forward slash:
""" test3a.py: open and read a file """
filepath = '/Users/david/Desktop/python_data/dir1/file1.txt'
fh = open(filepath)
print(fh.read())
(The above paths assume that the python_data folder is in the Desktop directory; your may have placed yours elsewhere on your system. Of course, the above paths also assume that my home directory is called david/; yours is likely different.)
This function joins together directory and file strings with slashes appropriate with the current operating system.
dirname = '/Users/david'
filename = 'journal.txt'
filepath = os.path.join(dirname, filename) # '/Users/david/journal.txt'
filepath2 = os.path.join(dirname, 'backup', filename) # '/Users/david/backup/journal.txt'
os.listdir() can read the contents of any directory.
import os
mydirectory = '/Users/david'
items = os.listdir(mydirectory)
for item in items: # 'photos'
item_path = os.path.join(mydirectory, item)
print(item_path) # /Users/david/photos/
# /Users/david/backups/
# /Users/david/college_letter.docx
# /Users/david/notes.txt
# /Users/david/finances.xlsx
Note the os.path.join() call. This is a standard algorithm for looping through a directory -- each item must be joined to the directory to ensure that the filepath is correct.
Several exceptions can indicate a file or directory misfire.
exception type | example trigger |
---|---|
FileNotFoundError | attempt to open a file not in this location |
FileExistsError | attempt to create a directory (or in some cases a file) that already exists |
IsADirectoryError | attempt to open() a file that is already a directory |
NotADirectoryError | attempt to os.listdir() a directory that is not a directory |
PermissionError | attempt to read or write a file or directory to which you haven't the permissions |
WindowsError, OSError | these exception types are sometimes raised in place of one or more of the above when on a Windows computer |
os.walk() visits every directory in a directory tree so we can list files and folders.
import os
root_dir = '/Users/david'
for root, dirs, files in os.walk(root_dir):
for tdir in dirs: # loop through dirs in this directory
print(os.path.join(root, tdir)) # print full path to tdir
for tfile in files: # loop through files in this dir
print(os.path.join(root, tfile)) # print full path to file
At each iteration, these three variables are assigned these values: