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’)
How do you remove the buffer from writing to a file?
fileID = open(‘file_name’,’w’,buffering = 0)
How do you set a specific number of bytes to buffer when using the write() command?
fileID = open(‘file_name’, ‘w’, buffering = x)
- x represents the number of bytes
How do you forcibly write buffered text into a file?
fileID.flush()
or
os.fsync(fileID)
What does the ‘with’ statement do?
opens a file, has indented code, then forcibly closes the file after the indented code has run
How do you split a string?
list = string.split(‘char’)
the split statement removes the character from the string when splitting
What does an empty .split() statement do
Split a string by white spaces including new lines
What does .split(‘\n’) do?
Splits the string at a new line, if there is no string after the new line then the data stored in the last index of the new list is empty ‘’
What does .join() do?
The inverse of split, it takes the data in a list and joins them together with some kind of separator
list1 = [‘hello’, ‘its’, ‘me’]
separator = ‘/’
string = separator.join(list1)
output = hello/its/me
What is a csv?
comma separated values, instead of separating data into rows and columns the rows are separated by commas (each row is one string)
- A piece of data between the commas in a csv are called fields
How do you read row by row from a csv file?
import csv
with open(‘file.csv’,’r’) as csv_file:
reader = csv.reader(csv_file)
for row in reader:
print(row)
- This will print each row of data from the csv file
- by default csv.reader reads the fields between the commas, if there is a different separator then you can add a delimiter to change what the csv.reader uses to separate fields:
reader = csv.reader(csv_file, delimiter = ‘;’)
Can you write to a csv file?
Yes, when you call the file just use ‘w’ as the delegator
How do you write each element of a list to each line of a file?
fileID.writelines(list)
- list must be a string