Reading and Writing Flashcards
Why work with files?
Files store data on non-volatile (data doesn’t disappear when program ends) storages media
By reading and writing files, programs can save information btwn program runs
Opening and Closing a File
general open form:
open(filename, mode)
filename is an external storage location
modes:
‘r’ = reading
‘w’ = writing
‘a’ = appending
to close file:
myfile. close()
- tells system that we are done writing and makes disk file available for reading or writing by other programs
Writing to a File
myfile = open(‘text.txt’, ‘w’)
- if no file named ‘text.txt’ on disk, it will be created
- if already one, will be replaced by file we are writing
- file assigned to object myfile that knows how to get info from file
method:
myfile. write(‘CATS!…’)
- does not attach a new line character by default
myfile. write(‘\n’)
- everytime we use myfile.write string added to file ‘text.txt’ where we left off
RECIPE:
myfile = open(filename, ‘w’)
myfile.write(‘string’)
myfile.close()
Reading a File
myfile = open(‘test.txt’, ‘r’)
- if file doesn’t exist ERROR
approached of read methods: 1. read - When you want to read the whole file at once and use it as a single string. myfile = open(filename, 'r') contents = myfile.read() myfile.close()
- readline
- when you want to process the file line-by-line
myfile = open(filename, ‘r’)
line = myfile.readline()
contents = ‘’
while line:
contents += line
line = myfile.readline()
myfile.close() - for line in file
- When you want to process the file line-by-line
myfile = open(filename, ‘r’)
contents = ‘’
for line in myfile:
contents += line
myfile.close() - readlines
- When you want to process the file line-by-line with an index
myfile = open(filename, ‘r’)
lines = myfile.readlines()
myfile.close()
with statement
with statement automatically closed file when the end of with block is reached
general form:
with open(filename, mode) as variable:
body
why use CSV Files?
text date commonly organized in spreadsheeet format using columns and rows
commonly done with comma-separated values (CSV)
Reading CSV Files
- read each row of CSV file using open
import csv
csvfile = open(‘grades.csv’. ‘r’)
grades_reader = csv.reader(csvfile)
row_num = 1 for row in grades_reader: print('Row #', row_num, ':', row) row_num += 1 csvfile.close()
- read each row of CSV file using with
import csv with open('grades.csv', 'r') as csvfile: grades_reader = csv.reader(csvfile)
row_num = 1
for row in grades_reader:
print(‘Row #’, row_num, ‘:’, row)
row_num += 1
Writing CSV Files
create CSV write object:
csv.writer()
can use writerow() method to populate CSV writer object w data
- can only write a single row to file at a time
import csv
rows = [[‘Name’, ‘Test1’, ‘Test2’, ‘Final’],
[‘Kendrick’, ‘100’, ‘50’, ‘69’],
[‘Dre’, ‘76’, ‘32’, ‘53’],
[‘Snoop’, ‘25’, ‘75’, ‘95’]]
with open('grades_new.csv', 'w') as csvfile: grades_writer = csv.writer(csvfile)
for row in rows: grades_writer.writerow(row)
Opening CSV File in Excel
to prevent new line from forming add parameter newline = ‘’
with open(‘grades_new.csv’, ‘w’, newline = ‘’) as csvfile: