cpsc 236 files

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/40

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 9:00 PM on 3/29/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

41 Terms

1
New cards

open function

open(file, mode)

2
New cards

close function

close()

3
New cards

read method

file object method that reads entire file contents into memory, only works if file has been opened for reading, contents returned as a string

4
New cards

read position

marks the location of the next item to be read from a file

5
New cards

reading data from a file

f = open(“demofile.txt“, “r“)

print(f.read())

6
New cards

returns the 5 first characters of the file

f = open(“demofile.txt“, “r“)

print(f.read(5))

7
New cards

seek

f=open(“test.txt“, “r“)

f.seek(3)

f.read()

starts reading from the 3rd character

8
New cards

readline method

file object method that reads a line from the file, line returned as a string including ‘\n’

9
New cards

read one line of the file

f = open(“demofile.txt“, “r“)

print(f.readline())

10
New cards

read two lines of the file

f = open(“demofile.txt“, “r“)

print(f.readline())

print(f.readline())

11
New cards

readlines() method

returns a list of strings, where the first element is the contents of the first line, the second element is the contents of the second line, and so on

12
New cards

readlines syntax

f = open(“demofile.txt“, “r“)

print(f.readlines())

13
New cards

open a file in write mode and close manually

outfile = open(“text.txt“, “w“)

outfile.write(“Test“)

outfile.close()

14
New cards

str function

converts value to string

15
New cards

for loop with file syntax

for line in file_object:

statements

16
New cards

opens a text file in write mode and automatically closes it

with open(“text.txt“, “w“) as outfile:

outfile.write(“Test“)

17
New cards

code that opens a text file in read mode and automatically closes it

with open(“test.txt“, “r“) as infile:

print(infile.readline())

18
New cards

write method of a file object

write(str)

19
New cards

write one line to a text file

with open(“members.txt“, “w“) as file:

file.write(“John Cleese \n“)

20
New cards

append one line to a text file

with open(“members.txt“, “a“) as file:

file.write(“Eric Idle \n“)

21
New cards

use a loop to read each line of the file

with open(“members.txt“) as file:

for line in file:

print(line, end=””)

print()

22
New cards

read the entire file as a string

with open(“members.txt“) as file:

contents = file.read()

print(contents)

23
New cards

read entire file as a list

with open(“members.txt“) as file:

members = file.readlines()

print(members[0], end=””)

print(members[1])

24
New cards

read each line of the file

with open(“members.txt“) as file:

member1=file.readline()

print(member1, end=””)

member2=file.readline()

print(member2)

25
New cards

write the items in a list to a file

members = [“John Cleese“, “Eric Idle“]

with open(“members.txt“, “w“) as file:

for m in members:

file.write(m, “\n“)

26
New cards

read line in a file into a list

members = []

with open(“members.txt“) as file:

for line in file:

line = line.replace(“\n“, ““)

members.append(line)

print(members)

27
New cards

write the items in a list to a file

years = [1975, 1979, 1983]

with open(“years.txt“, “w“) as years_file:

for year in years:

years_file.write(str(year) + “\n“)

28
New cards

read items in a list from a file

years = []

with open(“years.txt”) as file:

for line in file:

line = line.replace(“\n“, ““)

years.append(int(line))

print(years)

29
New cards

writer function for csv

writer(file)

30
New cards

writerows method for csv

writerows(rows)

31
New cards

write the list to a csv file

with open(“movies.csv“, “w“, newline=””) as file:

writer = csv.writer(file)

writer.writerows(movies)

32
New cards

reader function for csv

reader(file)

33
New cards

read data from csv file

with open(“movies.csv“, newline=””) as file:

reader = csv.reader(file)

for row in reader:

print(row[0] + “ (“ + str(row[1]) + “)“)

34
New cards

pickle module

binary protocols for serializing and de-serializing a python object, two main methods of dump and load

35
New cards

dump method

dumps an object to a file object, dump(object, bfile)

36
New cards

load method

loads an object from a file object, load(bfile)

37
New cards

write an object to a binary file

with open(“movies.bin“, “wb“) as file:

pickle.dump(movies, file)

38
New cards

read an object from a binary file

with open(“movies.bin“, “rb“) as file:

movie_list = pickle.load(file)

print(movie_list)

39
New cards

os module

provides an interface to operating system function calls

40
New cards

os module syntax

import os

myfile=open(“myfile.txt“, “r“)

file_info = os.stat(“mfile.txt“)

os.remove(“myfile.txt“)

41
New cards

os.walk()

“walks“ a directory tree, visiting each subdirectory in the specified path

Explore top notes

Explore top flashcards

flashcards
AP Psych Semester 1
350
Updated 471d ago
0.0(0)
flashcards
ch.3.1/3.2 Terms- Env Sci
24
Updated 930d ago
0.0(0)
flashcards
POCUS -Intro
47
Updated 248d ago
0.0(0)
flashcards
Spanish 3 Midterm
201
Updated 1199d ago
0.0(0)
flashcards
Chapter 21
61
Updated 1054d ago
0.0(0)
flashcards
AP US History Unit 3
69
Updated 517d ago
0.0(0)
flashcards
AP Psych Semester 1
350
Updated 471d ago
0.0(0)
flashcards
ch.3.1/3.2 Terms- Env Sci
24
Updated 930d ago
0.0(0)
flashcards
POCUS -Intro
47
Updated 248d ago
0.0(0)
flashcards
Spanish 3 Midterm
201
Updated 1199d ago
0.0(0)
flashcards
Chapter 21
61
Updated 1054d ago
0.0(0)
flashcards
AP US History Unit 3
69
Updated 517d ago
0.0(0)