Reading and Writing Flashcards

1
Q

Why work with files?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Opening and Closing a File

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Writing to a File

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Reading a File

A

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()
  1. 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()
  2. 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()
  3. readlines
    - When you want to process the file line-by-line with an index
    myfile = open(filename, ‘r’)
    lines = myfile.readlines()
    myfile.close()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

with statement

A

with statement automatically closed file when the end of with block is reached

general form:
with open(filename, mode) as variable:
body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

why use CSV Files?

A

text date commonly organized in spreadsheeet format using columns and rows

commonly done with comma-separated values (CSV)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Reading CSV Files

A
  • 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Writing CSV Files

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Opening CSV File in Excel

A

to prevent new line from forming add parameter newline = ‘’

with open(‘grades_new.csv’, ‘w’, newline = ‘’) as csvfile:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly