Reading and Writing Files
File access in Python is managed through a file object obtained from open.
The object holds an operating-system resource that must be released; leaving it
open risks exhausting the process's file handles and, for files opened for
writing, losing buffered data that was never flushed.
The with statement guarantees release. It closes the file when the block ends,
including when the block exits because of an exception. Opening a file outside a
with block, without a matching close, is a defect.
gene control_1 control_2 treated_1 treated_2
Several files are already mounted in this sandbox, listed in the Files tab.
counts.tsv and variants.tsv are used in the examples below.
Reading line by line
Iterating over a file object yields one line at a time and reads only as much as required, so it works on files far larger than available memory. This is the standard approach for large datasets, as high-throughput sequencing output routinely exceeds available physical RAM.
Each line retains its trailing newline, so strip is almost always applied
before use.
chrom pos ref alt gene qual chr17 7676154 G A TP53 99.0 chr17 43093220 C T BRCA1 45.2 chr7 55191822 T G EGFR 88.7 chr12 25245350 C T KRAS 12.4 chr7 140753336 A T BRAF 76.1
Reading the whole file
read returns the entire contents as one string, and readlines returns a list
of lines. Both load the whole file into memory and are appropriate only when the
size is known to be small.
7 lines gene control_1 control_2 treated_1 treated_2
splitlines is preferred over split("\n") because it removes the line
terminators and handles the carriage-return conventions of files produced on
other platforms.
Parsing delimited data
A tab-separated line becomes a list of fields with split("\t"). Read the
header row first, then use its index positions to locate columns by name rather
than by a hard-coded number, so that the code survives a change of column order.
TP53 2310 BRCA1 441 EGFR 1502 KRAS 1760 MYC 900 GAPDH 15100
Writing
The second argument to open is the mode. "r" reads and is the default;
"w" creates the file or truncates an existing one; "a" appends. "w"
discards existing contents without warning.
gene depth TP53 120
write does not append a newline; it must be supplied explicitly. Note that
the sandbox filesystem is temporary and is discarded when the page reloads.
Task
Write column_sum(path, column). Read a tab-separated file with a header row
and return the sum of the named column as an integer. Assume the column exists
and every value in it is a whole number.
Locate the column with header.index(column) rather than assuming a position,
and skip blank lines before splitting, since splitting an empty line produces a
single empty field that will not convert to an integer.
Test cases · 2
| # | via | input | expected stdout |
|---|---|---|---|
| 1 | stdin | control_1 | 20910 |
| 2 | stdin | treated_2 | 22306 |
Hints · 2
01Hint
Read the header first, then use header.index(column) to find the position.
02Hint
Skip blank lines before splitting, or the split will produce an empty field.