Quiz 9: JSON, CSV, and Directories
Learning Goals
JSON
Students should be able to:
- use
json.loadto read a Python value from a file - use
json.dumpto write a Python value from a file - understand that both
json.loadandjson.dumpwork on file objects, not file names (strings).- it is necessary to call
json.loadon the result of opening a filename for reading. - the second argument to
json.dumpmust be the result of opening a filename for writing. (The first argument ofjson.dumpis the Python value to be written).
- it is necessary to call
- understand that only a subset of Python values can be written to json:
None, booleans, numbers, strings, list, and dictionaries. Some notes:- tuples will be converted to lists
Nonewill be converted tonullTrueandFalsewill be converted totrueandfalse- In JSON, string literals must use double-quotes, not single quotes!
CSV
Students should be able to:
-
use
csv.DictReaderto read the rows as of a CSV file as a list of dictionaries whose keys are taken from the first (header/title) row of the file, and whos values are taken from the remaining rows.-
Understand that if
readeris the result of a call tocsv.DictReaderon a CSV file, then both of the following will return a list of dicitionaries for the rows:list(reader)[dct for dct in reader]
-
-
use
csv.DictWriterto write a list of dictionaries to a CSV file.- In addition taking a file object as its argument,
csv.DictWritermust also take afieldnameskeyword argument that specifies the order of the comma-separated title strings in the header line of the file. -
Understand that if
writeris the result of a call tocsv.DictWriter, then-
writer.writeheader()will write a comma-separated row of the fielnames to the CSV file. -
writer.writerow(dictRow)will write a comma-separated row of the values indictRowto the CSV file. writer.writerows(dictRows)will write comma-separated rows of the values for each dictionary indictRows.
-
- In addition taking a file object as its argument,
- understand that both
csv.DictReaderandcsv.DictWritework on file objects, not file names (strings).- it is necessary to call
csv.DictReaderon the result of opening a CSV filename for reading. - it is necessary to call
csv.DictWriteron the result of opening a CSV filename for writing.
- it is necessary to call
Directories
Students should be able to:
- Understand that data is perstistently stored on a computer in collections of files and folders arranged in tree, where the files are leaves of the tree and where directories (a.k.a. folders) are intermediate nodes in the tree. The contents of a directory/folder can include both files and other folders.
-
Use the following functions to operate on folders and files:
Function Description os.getcwd()Outputs the working directory as an absolute path os.listdir(pathName)Returns the list of files and folders for the given path name os.path.exists(pathName)Returns a boolean to indicate if a file or folder with the given pathname exists os.path.isfile(pathName)Returns a boolean to indicate if the given path name refers to a file os.path.isdir(pathName)Returns a boolean to indicate if the given path name refers to a folder os.path.join(folderPath, filename)Returns a string by joining together the given parameters. This new string is a valid path name. os.path.basename(pathName)Returns a string that corresponds to the last part in a path name. os.path.getsize(pathName)Returns a number that corresponds to the size in bytes of the file (or folder) referred by the path name. -
Understand that in the above functions,
pathNamemust be a relative pathname from the current directory directory. Otherwise the the functions will not work as expected. -
Understand that
os.listdir(pathName)returns a list of just the names of files and subdirectories in a directory withoutpathNameas a prefix. In almost all cases, it is necessary to useos.path.join(pathName, fileOrDirName)for eachfileOrDirNamereturned byos.listdir(pathName). -
Understand that the elements of in the list of strings return by
os.listdir(pathName)are not guaranteed to be sorted, so it is usually wise to wrapsortedaround a call toos.listdir. - Understand that the result returned by
os.path.getsizeon a directory is just the size of metadata in the directory, not the sum of the sizes of files/subdirectories contained in that directory.
Study Guide
Solutions
After your quiz is graded by instructors, you will get an email from Gradescope. Solutions for individual quiz questions are provided in the Gradescope rubric.