Lecture 10 Revision Flashcards

1
Q

What function do you assign your file variable to?

A

Create a variable. And assign it to be an open function.

The open function has 2 parameters:
filename and mode

The filename is a string giving an absolute or relative
path to the file location (remember absolute is starting at the root of the system, relative is to the current file you are accessing)

The mode is the method in which the file should be opened

syntax
myFile = open(filename, mode)

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

Why do you have to check the path seperator? And how do you do it.

A

Just because we are creating the script on a machine with one type of OS does not mean we will only use the script on that type of OS.

Different OS use different path seperators. So we need to find out which path seperator the system uses that we are going to execute the script on.

Python has an inbuilt module called os

import os
then you can call on os.sep to tell you what the current OS uses as a seperator.

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

What are the different file opening modes mean?

A

r’ read-only mode

‘w’ write-only mode (this will overwrite the data held in the file already)

‘a’ appending mode, write-only - appends new data onto the end of the original data

‘r+’ opened for both reading and writing (only for files that already exist)

‘w+’ opened for both reading and writing (if file doesn’t exist already it will create one)

‘a+’ opened for both reading and writing in appending mode. (if file doesn’t exist already it will create one)

‘b’ binary mode (to open a file that exists as binar

‘t’ text mode (e/g to open a text file, csv file etc)

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

Reading text files: What does this code do?

f = open(‘logs.dat’, ‘r’)
lines = f.readlines()
print lines
f.close()

A

Reads a text file:

first creates the variable called f

then the function open, looks for the file logs.dat which is in the same location as the script is being run from and opens it in r mode (read only)

then we create a variable called lines. This is assigned to be f.readlines() which will retrun back to us a list with each element of the list being a line of text from the file.

print lines will print the list, so it will output all the data contained in the file line by line.

f.close() closes the file. MUST DO THIS otherwise you will lock it and the file can’t be written to.

So in summary it reads the entirity of a file and puts it into a list for us.

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

Reading text files: What does the function
input.read()
do?

A

It reads the entirity of a file and stores in all as one block in one variable

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

Reading text files: What does the function
input.read(N)
do?

A

N is a number for the number of bytes you want to read. So it is the amount of data you wanrt to read expressed in BYTES. Most text files will be encoded in ASCII or UTF8 or UNICODE where one character is 8 bits, so can losley assume one byte = one character (changes with multibyte characters.)

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

Reading text files: What does the function
input.readline()
do?

A

Will return back to us the first line on that file

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

Reading text files: What does the function
input.readlines()
do?

A

Will read the entire file line by line but will store each line as an element in a new list that we are storing in whatever variable we have created

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

Writing text files: What will this example do?

output = open(‘test.txt’, ‘w’)
output.write(aString)

A

Writing text files:

Create the variable output.

Then the function open, looks for the test.txt which is in the same location as the script is being run from and opens it in w mode write-only mode (this will overwrite the data held in the file already and the file must already exist in this example.

Then we call on the function ouput.write and parse it through our variable called aString. This takes whatever is in our variable aSting and write it out into the test.txt file.

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

Writing text files: What will this example do?

output = open(‘test.txt’, ‘w’)
output.writelines(aList)

A

as above but it takes in an entire list, a list data structure and will process the list element by element and output to the test.txt file.

Again we are overwriting any data already on the file.

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

How do you close a file so it can be used by other users or programs?

A

filename.close()

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

How do you go to a specific position in the file?

A

filename.seek(N)

With N being the number of bytes

Remember files opened in r or w will default to the curser / pointer being at the start, files opened

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

How do you find the curser / pointer position in a file?

A

filename.tell

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

What does output.flush do?

A

Force information to be written immediately to the file

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

What is the shutil?

A

shutil is the Shell utility library

17
Q

How do you remove a file?

A

import os
os.remove(‘old.txt’)

18
Q

What are 4 x useful shutil operations for files

A

Need to import shutil first

Then use:

To copy: (works on binary or text)
shutil.copy(‘old_file_name.txt’, ‘new_file_name.txt’)

Move file:
shutil.move(‘first_file.text’, next_file.text’)
This is basically renaming a file which essentially is moving the absolute path of it.

Copy directory:
shutil.copytree(‘original_folder’, ‘new_copy_folder’)
Like right clicking copy

Remove a directory (and all files / folders in it):
shutil.rmtree(‘directory_name’)

19
Q

Recap: What is a VARIABLE?

A

A variable is a named storage location in memory that holds a value.

For example:
message = ‘Hello’

20
Q

Recap: what is a FUNCTION?

A

A function is a named reusable block of code designed to perform a specific task. A function can take data as input and provide data as output.

For example:
len(message)
returns 5

21
Q

What is an OBJECT?

A

An object is a storage location in memory that holds data and has specified behaviour associated with it.

22
Q

How are OBJECTS uniquely identified?

A

Every object is uniquely identified by its memory location.

23
Q

What do OBJECTS consist of?

A

An object has:
- Attributes (data): Every attribute has a name and a value.

  • Methods (behaviours): Every method has a name and code that is performed when it is called.
  • Class reference: Every object is defined by a named class. The class contains code for the attributes and the methods.

(Technically speaking a LIST is also an OBJECT)

24
Q

What is a CLASS?

A

You can also say that an object is an instance of a CLASS.

A CLASS is named code which definies the object’s ATTRIBUTES and METHODS

25
What happens when an OBJECT is created?
When an object is created: - memory space is allocated - the attributes are given initial values - the initial method in the class is run When an object's method is called: - the method's code is executed Python automatically deletes an object when there are no more references to it See lecture diagrams to understand better
26
Another way to explain OBJECTS and CLASSES
Classes are like blueprints for creating objects. They define what properties (attributes) and actions (methods) the objects created from them will have. Objects are instances of classes. Think of objects as the actual things you create using the blueprint (class). Here's an analogy: Imagine you're building a house. The blueprint (class) tells you what the house should look like, how many rooms it has, where the windows and doors are, etc. Each house you build from that blueprint is an object.