Skip to main content
Engineering LibreTexts

7: Files

  • Page ID
    122365
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    In this chapter, we start to work with Secondary Memory (or files). Secondary memory is not erased when the power is turned off. Or in the case of a USB flash drive, the data we write from our programs can be removed from the system and transported to another system.

    • 7.1: Persistence
      This page discusses the transition from primary to secondary memory in programming, highlighting the volatility of primary memory versus the persistent nature of secondary memory for file storage. It focuses on reading and writing text files and indicates that future discussions will cover binary files used in databases, emphasizing the importance of this shift for creating programs with lasting impact beyond transient Python coding exercises.
    • 7.2: Opening Files
      This page discusses the process of opening a file on a hard drive, including checking for its existence and access permissions. Upon successful opening, a file handle is provided to read data. Errors may occur if the file does not exist, and future content will focus on improving error handling using try and except statements.
    • 7.3: Text files and Lines
      This page explains that a text file operates as a sequence of lines, analogous to a Python string as a sequence of characters. It highlights the use of the mbox format for tracking email exchanges in projects, with "From " lines marking message separations. The newline character (\n) indicates line endings in Python, emphasizing its importance in managing text files since it defines line boundaries even though it's not visible in the file.
    • 7.4: Reading Files
      This page explains reading and counting lines in a file using a for loop in Python. It highlights the efficiency of line-by-line reading for large files, preserving memory by processing one line at a time. It contrasts this with the `read` method, suitable for small files, which loads the entire content into a string for easier manipulation, advising caution to ensure the file's size fits into memory.
    • 7.5: Searching through a File
      This page discusses file reading techniques, focusing on filtering lines that start with "From:", handling extra blank lines, and removing trailing whitespace using the `rstrip` method. It explains how to use the `continue` statement to skip uninteresting lines and utilizes the `find` method for substring searches. The content illustrates the construction of simple search mechanisms in file processing.
    • 7.6: Letting the user choose the file name
      This page explains how to enhance a Python program for dynamic file input, allowing users to process various files without altering the code. It includes an example of reading a file and counting lines that begin with 'Subject:'. The program prompts for a file name, opens it, and reports the count of subject lines while also addressing possible user input issues that could lead to crashes.
    • 7.7: Using try, except, and open
      This page emphasizes the significance of managing user input errors in software development, particularly in Python. It highlights how trying to open a non-existent file can cause traceback errors and recommends using try/except blocks for error handling and better user feedback. The text also underlines the importance of Quality Assurance (QA) teams in catching issues pre-delivery and advocates for a balance between functionality and elegance, aiming for a "Pythonic" solution.
    • 7.8: Writing Files
      This page explains how to write to a file in Python, highlighting the need to open the file in write mode ('w') which clears existing data. It discusses the use of the write method, noting that newline characters must be added manually. The importance of closing the file after writing to ensure data is saved is emphasized, despite Python automatically closing files at program termination. Explicitly closing files is recommended to prevent data loss.
    • 7.9: Debugging
      This page discusses whitespace issues in files caused by invisible characters, which can lead to errors. It highlights the `repr` function for revealing these characters and notes that different systems have varying newline representations that can complicate file transfers. It also mentions the availability of applications for format conversion and the possibility of creating custom solutions.
    • 7.E: Files (Exercises)
      This page presents three programming exercises focused on file handling and user interaction. Exercise 1 involves creating a program to read a file and print its contents in uppercase. Exercise 2 entails extracting and averaging "X-DSPAM-Confidence" values from a file. Exercise 3 introduces humor with a special file name prompt, "na na boo boo," while performing normally for other names. Each exercise includes execution examples to guide learning and exploration in programming.
    • 7.G: Files (Glossary)
      This page covers key programming concepts including the "catch" mechanism for error handling in Python, the role of "newline" in delimiting lines in files, the notion of "Pythonic" practices for effective coding, the importance of "Quality Assurance" in software testing, and defines a "text file" as a sequence of characters stored permanently.


    This page titled 7: Files is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Chuck Severance via source content that was edited to the style and standards of the LibreTexts platform.