Module 10 - File Access Flashcards

1
Q

What do you call a program that runs all the time and keeps at least some of its data in permanent storage?

A

A persistent program

example: operating systems and web servers

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

How do you read a line from a text file? Write the sample code.

A

use open function and assign to variable:
fin = open (‘words.txt’)
then use readline method:
fin.readline ( )

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

What is a text file?

A

A sequence of characters stored on a permanent medium like a hard drive, flash memory, or CD-ROM

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

How do you write a file?

A

You have to open it with mode ‘w’ as a second parameter:

fout = open(‘output.txt’, ‘w’)

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

What are the two types of files? What is the difference?

A

Text file - contains data that has been encoded as text

Binary file - contains data that has not been converted to text

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

What are the two types of access to data stored in a file?

A

Sequential access - file read sequentially from beginning to end, can’t skip ahead
Direct access - can jump directly to any piece of data in the file

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

What is a filename extension and what does it indicate?

A

Short sequence of characters at the end of filename (after the period)
Indicate the type of data stored in the file

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

What’s a file object? What’s the point of it?

A

Object associated with a specific file

Provides a way for a program to work with the file: file object referenced by a variable

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

What’s the ‘open’ function used for and what does it do? What’s the syntax?

A

Used to open a file
It creates a file object and associates it with a file on the disk

file_object = open (filename, mode)

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

What are the different modes of the ‘open’ function?

A

Mode is the second parameter in the function
Reading only - ‘r’
Writing - ‘w’
Appending - ‘a’

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

What happens if the open function receives a filename that does not contain a path?

A

It assumes that file is in the same directory as the program

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

If a program is running and a file is created, where is it created? Can you alter this?

A

In the same directory as the program
Can specify alternative path and file name in the open function argument - prefix the path string literal with the letter r

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

What is a method?

A

A function that belongs to an object

Performs operations using that object

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

How do you write data to a file?

A

File object’s write method

file_variable.write(string)

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

How do you close a file?

A

File object’s close method

file_variable.close( )

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

What’s the return value of the write method?

A

The number of characters that were added (i.e., ‘24’)

17
Q

What does ‘read’ do? How does it work?

A

Reads entire file contents into memory
Works only if file has been opened for reading
Contents return as a string

18
Q

What’s ‘read position’ do?

A

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

19
Q

What happens to the data if you open an existing file in ‘w’ mode?

A

It is overwritten

Use ‘a’ (append) to add to end of current contents

20
Q

What happens if you try to append a file that does not exist?

A

The ‘a’ mode will create a new file

21
Q

T or F: Numbers must be converted to strings before they are written to a file

A

True
Use str function
Then use int or float functions to convert back (to perform math)

22
Q

How can you use a while loop to read a file?

A

Use the readline method

while line != “ “

23
Q

How can you use a for loop to read a file?

A

for line in file_object:

___statements

24
Q

What is a record?

A

Set of data that describes one item

25
Q

What is a field?

A

Single piece of data within a record

26
Q

How do you write and read a record to/from a file?

A

Write record to sequential access file by writing the fields one after the other
Read record from sequential access file by reading each field until record complete

27
Q

What is the difference between write and writelines methods?

A

WriteLine() provides a new line character \n at the end of the string

28
Q

How do you remove the \n character at the end of the writelines method?

A

rstrip method

29
Q

What is the difference between read and readlines methods?

A

Reads entire file contents into memory vs. reads a line from the file (line returned as string ending in ‘\n’)

30
Q

Open the file hostdata.txt for reading.

A

open (‘hostdata.txt’, ‘r’)

DON’T FORGET QUOTES!

31
Q

How do you create a file object from a file?

A

Use the open function

It creates a file object, assigns it to a variable and associates it to the file

32
Q

A file named numbers.txt contains an unknown number of lines, each consisting of a single integer. Write some code that computes the sum of all these integers, and stores this sum in a variable name sum.

A

fin=open(‘numbers.txt’,’r’)
sum=0
for line in fin:
____sum+=int(line)

  1. create file object (open function)
  2. initialize sum variable
  3. create for loop (it will iterate over every line in the file)
  4. add each line to the sum variable (remember to CONVERT to integer!!)