coding with files

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

1/22

flashcard set

Earn XP

Last updated 12:46 AM on 11/24/22
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

23 Terms

1
New cards
one diagram for how to open a file
knowt flashcard image
2
New cards
designator for reading
r
3
New cards
designator for writing
w
4
New cards
designator for appending
a
5
New cards
designators for reading/writing/appending binary data
rb, wb, ab
6
New cards
as a designator
r (for reading)
7
New cards
designator for reading and writing to a file
r+
8
New cards
one diagram of how to close a file
knowt flashcard image
9
New cards
one diagram of how to operate on a file with one line of non-indented code
knowt flashcard image
10
New cards
how to write to a file after it has been opened
knowt flashcard image
11
New cards
how writing on to a file works
write will only write a single string
write will write only strings
write will not put a carriage return/new line after writing
(You will need to explicitly put in a newline character (\n) if you want a
newline)
12
New cards
how to open a file in a different directory
knowt flashcard image
13
New cards
how to read from a file one line at a time (this uses a text file)
= .readline() example: next_line = myfile.readline()
14
New cards
how to read multiple lines in a file OPTION 1
for <lineID> in <fileID>:
#do stuff with the string lineID
15
New cards
how to read multiple lines in a file OPTION 2
next_line = myfile.readline()
while next_line != ' ':
# Do stuff with the string next_line
next_line = myfile.readline()
16
New cards
example of reading from and printing a file
knowt flashcard image
17
New cards
how to read from a file in one single string
= .read()
18
New cards
how to convert a file (in code) into a list of strings, with each component representing one line of text from the file
= list() or = .readlines()
19
New cards
how to convert a string into a list of smaller strings, separated by a given character
= .split()
20
New cards
s = "1,2,3,4"
elems = s.split(',')
print(elems)
['1', '2', '3', '4']
21
New cards
how to remove leading and trailing whitespace
.strip()
22
New cards
how to concatenate a list of strings using a separator
.join()
23
New cards
example of string methods being used
knowt flashcard image