2.9: Files
- Page ID
- 206267
\( \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}\)hypothes.is tag: s20iostpy09ualr
Download Assignment: S20py09
Learning Objectives
Students will be able to:
Content:
- Explain how to open a text file for reading and for writing
- Explain the difference between the read() and readline() functions
- Explain the purpose of the str() function.
- Explain the effect of the arguments of the range function when reading data from a file.
- Explain the purpose of the rstrip(), open(), close(), and write() functions
- Explain the difference between writing and appending to a file
Process:
- Write code that opens, writes to and closes a file
- Write code that opens, reads from and closes a file
Prior Knowledge
- Python concepts from previous activities
Further Reading
Information: |
Task 1: Reading a File
Download the text file named inventory.txt and check its contents against the chemicals listed below, or pasting the list of chemicals into a new text file (named inventory.txt). Create a new program file by entering Python program below. Be sure the saved program is in the same folder as the text file. Run the program
Python Program 1 | inventory.txt file contains: |
|
methane |
Critical Thinking Questions:
- What does the program do?
- Explain the purpose and syntax of the first line of code in the user defined function main(). What does the ‘string argument’ for the function open represent?
list = open('inventory.txt')
- Replace the function read() with the function readline(). Execute the program again. Explain the difference between the two functions: read() and readline().
- Explain the difference between the two functions: read() and readline().
2. Enter and execute the following code.
Python Program 2 |
|
- How does the output from this program differ from the output of the program that used the read() function? What caused the difference?
- What is the subtle difference in the output if the following print statement replaced the one above? Which is better?
print(index,". ",chemical)
- What does str(index) do in the program above? Why is the str () function necessary?
- What happens when you change the arguments in the range() function to (1,10)?
- What happens when you change the arguments in the range() function to (0, 30)?
- What do the results from “d.” and “e.” tell you about the arguments of the range() function when you are reading data from a file with a for loop?
Note
The purpose of the rstrip() function is rather subtle. rstrip() returns a copy of the string after all characters have been stripped from the end of the string (default whitespace characters, EOL characters, and newline characters).
3. The following program is slightly different from Python Program 2. Enter and execute the program.
Python Program 3 |
|
- Compare the output from this program to the output in the previous program. What is the difference?
- What code caused the difference in the output?
- In this example the rstrip(\n) function strips the newline character (\n) from the string that is read from the file. Why did the newline character get attached to the end of the string in the first place?
- Change the line of code in the print statement to not have the \n as shown below.
list = open('inventory.txt')
Is it absolutely necessary to have the newline character be an explicit argument in this function?
- lstrip() is a similar function. What do you think it does? To test this try replacing rstrip() in your code with lstrip('p') and look at lines 3, 5, 13, and 15.
4. The following program is slightly different from Python Program 3. Enter and execute the program.
Python Program 4 |
|
- What does the program display?
- How many features are used in this program?
- Two functions use what is known as dot(.) notation. What are the two functions?
- Examine the output and explain what the len() function does.
5. Enter and execute the following program.
Python Program 5 |
|
- What output appears on the screen?
- What does the program do?
- Locate the file studentInfo.txt on your computer. The file is stored in the same folder as the program. What is stored in the file?
- What is the purpose of “w” in the following line of code?
inFile = open("studentInfo.txt", 'w')
- Did you have to create the file: studentInfo.txt before you included it in the program?
- Execute the program again using different input. Open the studentInfo.txt file. What is in the file? Is the data from the first program execution still there?
- Change the ‘w’ to ‘a’ in the open() function. Execute the program again with different input. Examine the student Info.txt file. What did ‘a’ as an argument in the open() function do? What word do you think ‘a’ represents
- Notice the new function – write(). How many arguments does the write function have?
- How does the write() function know what file to write to?
- What line of code closes the file? Where is it located with respect to other lines of code that interact with the file? Why do you think it is located there?
- Revise the program so that the user can enter three names during one execution of the program. You may need to change the order of some of the code. Be sure to only include the code that need to be repeated.
Application Questions:
- Create a text file that contains 10 numbers between 50 and 100. Write a program that reads the numbers from the file and totals the numbers. The program should print all the numbers and display the total when all the numbers have been added together. (Warning! The input from the file will be considered a string. Be sure to convert the input to int or float – just as you do when numbers are entered from the keyboard.
- Revise Python Program 3. Allow the user to enter the name of the file and use the input to open the file.
Python Program 3 |
|