Lecture 10 Revision Flashcards
What function do you assign your file variable to?
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)
Why do you have to check the path seperator? And how do you do it.
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.
What are the different file opening modes mean?
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)
Reading text files: What does this code do?
f = open(‘logs.dat’, ‘r’)
lines = f.readlines()
print lines
f.close()
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.
Reading text files: What does the function
input.read()
do?
It reads the entirity of a file and stores in all as one block in one variable
Reading text files: What does the function
input.read(N)
do?
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.)
Reading text files: What does the function
input.readline()
do?
Will return back to us the first line on that file
Reading text files: What does the function
input.readlines()
do?
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
Writing text files: What will this example do?
output = open(‘test.txt’, ‘w’)
output.write(aString)
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.
Writing text files: What will this example do?
output = open(‘test.txt’, ‘w’)
output.writelines(aList)
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 do you close a file so it can be used by other users or programs?
filename.close()
How do you go to a specific position in the file?
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 do you find the curser / pointer position in a file?
filename.tell
What does output.flush do?
Force information to be written immediately to the file
What is the shutil?
shutil is the Shell utility library
How do you remove a file?
import os
os.remove(‘old.txt’)
What are 4 x useful shutil operations for files
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’)
Recap: What is a VARIABLE?
A variable is a named storage location in memory that holds a value.
For example:
message = ‘Hello’
Recap: what is a FUNCTION?
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
What is an OBJECT?
An object is a storage location in memory that holds data and has specified behaviour associated with it.
How are OBJECTS uniquely identified?
Every object is uniquely identified by its memory location.
What do OBJECTS consist of?
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)
What is a CLASS?
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