Programming with Python · free preview
Analyzing Data from Multiple Files
Use a library function to get a list of filenames that match a wildcard pattern. Write a `for` loop to process multiple files.
1. Overview
Overview
2. Plotting Differences
Plotting Differences
3. Generate Composite Statistics
Generate Composite Statistics
4. Make file order deterministic
Which expression gives a deterministic alphabetical list of matching filenames?
5. Tracing a sorted glob and multi-file loop
Trace this complete Python program. It uses glob and sorted to select the first three matching data files, then prints each file's shape and mean. Predict the exact three output lines and explain what stays fixed across passes.
Example. Tracing a sorted glob and multi-file loop
Problem. Trace this complete Python program. It uses glob and sorted to select the first three matching data files, then prints each file's shape and mean. Predict the exact three output lines and explain what stays fixed across passes.
Correct solution.
import glob
import numpy
filenames = sorted(glob.glob('data/inflammation-*.csv'))[0:3]
for filename in filenames:
data = numpy.loadtxt(fname=filename, delimiter=',')
print(filename, data.shape, round(float(numpy.mean(data)), 6))Expected input, the files matching data/inflammation-*.csv, with the bundled data present.
Expected output.``output data/inflammation-01.csv (60, 40) 6.14875 data/inflammation-02.csv (60, 40) 5.990833 data/inflammation-03.csv (60, 40) 4.204583 ``
Trace/reasoning,glob.glob returns matching paths in arbitrary order, and sorted makes the selection reproducible. The slice [0:3] keeps exactly three filenames. Pass one loads inflammation-01.csv, so filename is data/inflammation-01.csv, data.shape is (60, 40), and the file mean is 6.14875. Pass two does the same for inflammation-02.csv and prints mean 5.990833. Pass three loads inflammation-03.csv and prints mean 4.204583. The old data value is replaced each pass, so no separate arrays accumulate unless you deliberately keep them.
关键提醒。
The trace makes the file order deterministic and separates the filename, the loaded array, and the computed summary. It also shows that axis is not needed for the whole-file mean but is needed for per-column statistics.
6. Retrieval practice
Without looking back, a directory contains exactly these files, data/inflammation-01.csv, data/inflammation-02.csv, data/inflammation-10.csv, and data/inflammation-13.csv. Write the Python expression that returns the matching filenames in reproducible alphabetical order, and explain why sorted is needed before processing the first three.
Answer with the glob pattern, the sorted expression, and the first three filenames your expression will select.
7. Mastery check
Apply the multi-file loop to a new input set, for data/inflammation-04.csv, data/inflammation-05.csv, and data/inflammation-06.csv, write a complete Python loop that prints each filename, data.shape, and the rounded whole-file mean. Then state the expected output for the third file.
Give the runnable loop and the exact expected third line, including shape and rounded mean.
Practice this interactively
Free account · instant grading · spaced review that schedules itself.
Start this course for free