Difference between revisions of "CSC111 Lab 9 2014"
(Created page with "--~~~~ ---- =Reading and Writing Text Files= ==Write Example== <br /> <source lang="python"> # write some variables to file name = "Smith College" address = "1 Elm st., Nort...") |
(→Writing Information to File) |
||
Line 26: | Line 26: | ||
</source> | </source> | ||
<br /> | <br /> | ||
− | =Writing | + | =Writing Text to File and Reading it Back= |
<br /> | <br /> | ||
<!-- ----------------------------------------------------------------------------------------------- --> | <!-- ----------------------------------------------------------------------------------------------- --> | ||
Line 54: | Line 54: | ||
* Once the program terminates, open the file with '''TextEdit''' if working on a Mac, or with '''Notepad''' if working on Windows. Verify that the file exists and that it contains the poem. | * Once the program terminates, open the file with '''TextEdit''' if working on a Mac, or with '''Notepad''' if working on Windows. Verify that the file exists and that it contains the poem. | ||
− | * Put the code that writes the poem to file in a function called ''' | + | * Put the code that writes the poem to file in a function called '''writeTextFile( fileName, text )''', where ''fileName'' is the name of the file, and ''text'' the name of the variable. The function should 1) open the file for writing, 2) write text to the file, and 3) close the file. |
− | + | Verify that your function works by deleting '''poem.txt''' and then running your new program. If you function is written correctly, it will have recreated the '''poem.txt''' file with the same contents as before. | |
<br /> | <br /> | ||
<!-- ----------------------------------------------------------------------------------------------- --> | <!-- ----------------------------------------------------------------------------------------------- --> | ||
Line 74: | Line 74: | ||
text = readTextFile( "poem.txt" ) | text = readTextFile( "poem.txt" ) | ||
print( text ) | print( text ) | ||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | =Writing Numbers to File and Reading them Back= | ||
+ | |||
+ | <br /> | ||
+ | Same idea: you write a function that takes a list of numbers and writes them to file, one per line. Then you write a function that reads the numbers back, prints them, and computes their sum. This last part is very important, because to text files contain text, or strings, and when you read that back into variables that are supposed to contain numbers, you must transform the strings into numbers. | ||
+ | |||
+ | Here is the list of numbers to use, and an example of how to organize your main program: | ||
+ | |||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | numbers = [ 1, 2, 10, 100, 50, 7 ] | ||
+ | |||
+ | writeIntFile( "numbers.txt", numbers ) | ||
+ | |||
+ | newNumbers = readIntFile( "numbers.txt" ) | ||
+ | for x in newNumbers: | ||
+ | print( x ) | ||
+ | |||
+ | print( "Sum = ", sum( newNumbers ) ) | ||
</source> | </source> | ||
<br /> | <br /> |
Revision as of 22:49, 1 April 2014
--D. Thiebaut (talk) 23:40, 1 April 2014 (EDT)
Contents
Reading and Writing Text Files
Write Example
# write some variables to file
name = "Smith College"
address = "1 Elm st., Northampton, MA 01063"
file = open( "college.txt", "w" )
file.write( "%s\n" % name )
file.write( "%s\n" % address )
file.close()
Read Example
# read the same file back and print all the lines
file = open( "college.txt", "r" )
for line in file:
print( "line =", line.strip() )
file.close()
Writing Text to File and Reading it Back
Challenge 1 |
- Use the example above and write a program that writes the string text defined below to a file named poem.txt in the same folder (directory) where your program is located.
text = """An Evening by Gwendolyn Brooks A sunset's mounded cloud; A diamond evening-star; Sad blue hills afar; Love in his shroud. Scarcely a tear to shed; Hardly a word to say; The end of a summer day; Sweet Love dead."""
- Once the program terminates, open the file with TextEdit if working on a Mac, or with Notepad if working on Windows. Verify that the file exists and that it contains the poem.
- Put the code that writes the poem to file in a function called writeTextFile( fileName, text ), where fileName is the name of the file, and text the name of the variable. The function should 1) open the file for writing, 2) write text to the file, and 3) close the file.
Verify that your function works by deleting poem.txt and then running your new program. If you function is written correctly, it will have recreated the poem.txt file with the same contents as before.
Challenge 2 |
- Add some code to your program that will read the file just created and print its contents (all the lines) on the screen.
- When this works, put this new code into a function called readTextFile() that is given just the file name (fileName) and that returns a long string that is equal to the contents of the file. Here is an example of how your program will use your new function to read the file and display the string:
text = readTextFile( "poem.txt" )
print( text )
Writing Numbers to File and Reading them Back
Same idea: you write a function that takes a list of numbers and writes them to file, one per line. Then you write a function that reads the numbers back, prints them, and computes their sum. This last part is very important, because to text files contain text, or strings, and when you read that back into variables that are supposed to contain numbers, you must transform the strings into numbers.
Here is the list of numbers to use, and an example of how to organize your main program:
numbers = [ 1, 2, 10, 100, 50, 7 ]
writeIntFile( "numbers.txt", numbers )
newNumbers = readIntFile( "numbers.txt" )
for x in newNumbers:
print( x )
print( "Sum = ", sum( newNumbers ) )