Chapter 11 (files) Flashcards
How do you access the contents of a file?
variable = open(‘file name’, ‘designator’)
designator tells the program how it uses the file
or
with open() as variable:
—- indented code
file will automatically closed after the indented code is finished
How do you read the contents in the file?
variable1 = open(‘file.txt’)
variable2 = variable1.read()
What is a file extension?
.pdf .txt .doc etc.
Hints to the computer how the contents of the file should be organized
What file designator allows you to append to a file?
‘a’
What file desginator allows you to read a file?
‘r’
What file designator allows you to write to a file?
‘w’
What file designator allows you to read and write to a file?
‘r+’
What are the file designators?
r = reading
w = writing to a new file or overwrite an existing file
a = appending (adding data)
rb/rw/ra = read/write/append binary data
r+ = read and write to the file
rb+ = read, write and binary file
How do you close a file?
variable.close()
How do you write into a file?
fileID.write(‘string’)
- or use string variable as a parameter
How do you search for a file for your code?
fileID = open(‘location1//location2//file.txt’,’designator’)
How do you loop through each line of a file?
for line_num in fileID:
—- indented code
How to put individual lines of a file into a list?
list1 = fileID.readlines()
or
list2 = list(fileID)
How do you read a certain number of bytes?
fileID.read(no.bytes)
Why is there a delay when using the write() command to a file?
There is a buffer before the text is written to the file, it is stored in memory then pasted into the file after a new line is written: write(‘\n’)