Chapter 11 (files) Flashcards

1
Q

How do you access the contents of a file?

A

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

How do you read the contents in the file?

A

variable1 = open(‘file.txt’)
variable2 = variable1.read()

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

What is a file extension?

A

.pdf .txt .doc etc.
Hints to the computer how the contents of the file should be organized

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

What file designator allows you to append to a file?

A

‘a’

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

What file desginator allows you to read a file?

A

‘r’

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

What file designator allows you to write to a file?

A

‘w’

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

What file designator allows you to read and write to a file?

A

‘r+’

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

What are the file designators?

A

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

How do you close a file?

A

variable.close()

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

How do you write into a file?

A

fileID.write(‘string’)
- or use string variable as a parameter

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

How do you search for a file for your code?

A

fileID = open(‘location1//location2//file.txt’,’designator’)

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

How do you loop through each line of a file?

A

for line_num in fileID:
—- indented code

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

How to put individual lines of a file into a list?

A

list1 = fileID.readlines()
or
list2 = list(fileID)

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

How do you read a certain number of bytes?

A

fileID.read(no.bytes)

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

Why is there a delay when using the write() command to a file?

A

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

How do you remove the buffer from writing to a file?

A

fileID = open(‘file_name’,’w’,buffering = 0)

17
Q

How do you set a specific number of bytes to buffer when using the write() command?

A

fileID = open(‘file_name’, ‘w’, buffering = x)
- x represents the number of bytes

18
Q

How do you forcibly write buffered text into a file?

A

fileID.flush()
or
os.fsync(fileID)

19
Q

What does the ‘with’ statement do?

A

opens a file, has indented code, then forcibly closes the file after the indented code has run

20
Q

How do you split a string?

A

list = string.split(‘char’)
the split statement removes the character from the string when splitting

21
Q

What does an empty .split() statement do

A

Split a string by white spaces including new lines

22
Q

What does .split(‘\n’) do?

A

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 ‘’

23
Q

What does .join() do?

A

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

24
Q

What is a csv?

A

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

25
Q

How do you read row by row from a csv file?

A

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 = ‘;’)

26
Q

Can you write to a csv file?

A

Yes, when you call the file just use ‘w’ as the delegator

27
Q

How do you write each element of a list to each line of a file?

A

fileID.writelines(list)
- list must be a string