Upsorn Praphamontripong CS 1110 Introduction to Programming Spring 2017 Overview File Input and Output 2 CS 1110 O pening files Reading from files Writing to files Appending to files ID: 610953
Download Presentation The PPT/PDF document "File Processing" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
File Processing
Upsorn
Praphamontripong
CS 1110
Introduction to Programming
Spring 2017Slide2
Overview: File Input and Output
2
CS 1110
O
pening
files
Reading from files
Writing to files
Appending to files
Closing filesSlide3
Opening Files (Local)
3
CS 1110
Opening a file
file_variable
= open(
filename
,
mode)The location of a fileIn general, we need to specify a path to a file. If a data file and your program are in different folders, an absolute path must be used. If a data file and your program is in the same folder, a relative path can be used. In this course, for homework submission/testing purpose and to avoid a “file not found” problem, you *need* to put all data files in the same folder as your python files and specify a relative path. Example test_file = open('test.txt', 'r') # relative path
‘r’ = read only
‘w’ = write (If the file exists, erase its contents. If it
does not exist, create a new file to be written to)
‘a’ = append a file (If the file exists, append data to the file.
If the file does not exist, create a new file)Slide4
Reading from Files: readline( )
4
CS 1110
Reading from a file
file_variable
= open(
filename
, 'r') file_variable.readline()def readline_file(): # open a file named students.txt to be read infile = open('students.txt'
,
'r'
)
# read 2 lines from the file
line1 = infile.readline() line2 = infile.readline() print(line1, line2) print(line1, line2) # close the file infile.close()Slide5
Reading from Files: readline( ) with loops
5
CS 1110
Reading from a file with loops
file_variable
= open(
filename
, 'r') file_variable.readline()def readline_file_with_loop(): # open a file named students.txt to be read infile = open('students.txt'
,
'r'
)
line =
infile.readline
()
while line != '': print(line.rstrip('\n')) line = infile.readline() # what happen if this line is commented out # close the file infile.close
()Slide6
Reading from Files: read( )
6
CS 1110
Reading from a file
file_variable
= open(
filename
, 'r') file_variable.read()def read_file(): # open a file named students.txt to be read infile = open('students.txt',
'r'
)
# read the file's contents
file_contents
= infile.read() print(file_contents) # close the file infile.close()Slide7
Writing to Files
7
CS 1110
Writing to a file
file_variable
= open(
filename
, 'w') file_variable = write(string_to_be_written)def main(): # open a file named students.txt outfile = open('students.txt'
,
'w'
)
# write the names of three students to the file
outfile.write('John\n') outfile.write('Jack\n') outfile.write('Jane\n') # close the file outfile.close()
John\
nJack
\
nJane
\n
Beginning of the file
End of the file
Where is this file? What does it look like?Slide8
Appending to Files
8
CS 1110
Appending to a file
file_variable
= open(
filename
, 'a') file_variable = write(string_to_be_written)def main(): # open a file named students.txt outfile = open('students.txt'
,
'a'
)
# write the names of three students to the file
outfile.write('Mary\n') # close the file outfile.close()John\nJack\nJane\
nMary
\n
Beginning of the file
End of the file
John\
nJack
\
nJane
\n
Beginning of the file
End of the file
Where is this file? What does it look like?Slide9
Closing Files
9
CS 1110
Closing a file
file_variable.close
()
def
read_file(): # open a file named students.txt to be read infile = open('students.txt', 'r') # read the file's contents file_contents = infile.read()
print(
file_contents
)
# close the file
infile.close()Slide10
Wrap Up File Operations
10
CS 1111
def
read_list_of_names
(filename):
names
= [] datafile = open(filename, "r") outfile = open(filename, "a") for line in datafile: line = line.strip() names.append(line) outfile.write(line) datafile.close() outfile.close() return names print(read_list_of_names("names.txt")) Function header (or signature)
argument
Function call
parameter
Open a file with a “read” mode
For each line in
datafile
Strip leading and tailing spaces
Close files
Value-return function
Open a file with an “append” mode
Write string to
outfile
(must be string)
Why isn’t a file opened with a “write” mode ?
If we want to open a file with a “write” mode,
how should we modify the code ?
Append string to a list
names Slide11
Validating a File
11
CS 1111
f
rom
os.path
import *
# get file name
file_name
= input("Enter csv file name: ")# check if a file is csv, is valid, and existswhile (not file_name.endwith("csv") or not isfile(file_name) ): file_name = input(file_name + " is not csv or does not exist. Enter csv file name: ")Slide12
Exercise (Local File)
12
CS 1111
Down the following file and save it as “
weather.csv
” in the same folder where you have your .
py
file
(Do not double click!! Do not open it with Excel!!)
Charlottesville Weather for September 2015 - http://cs1110.cs.virginia.edu/code/cville_weather_sept15.csvWrite a function to read the weather data and compute the average “Max TemperatureF” of Sep 2015. Hint: burn header, use split(“,”), and cast dataSlide13
Opening Files (Internet, via URLs)
13
CS 1111
import
urllib.request
link
= input ( 'Web page: ' )
stream
= urllib.request.urlopen( link ) for line in stream: decoded = line.decode("UTF-8") print(decoded.strip())Slide14
Exercise (URL)
14
CS 1111
Write a function to read a dataset from the Internet
and display the data on the screen.
This dataset contains history temperature.
(
Do not save the dataset to your computer
)http://www.wunderground.com/history/airport/KCHO/2012/03/17/DailyHistory.html?format=1Hint: use urllib, decode("UTF-8"), strip(), then split()