File handling¶
Definition¶
In order to read or write to files we use the following process:
- Open the file
- Read or write to the file
- Close the file
Note
There are different modes in which you can open files. The most common are read only mode (you cannot write to the file), write mode (you can write to the file, existing contents are deleted) and append mode (adds content to the end of a file without deleting any existing contents).
Easy example¶
file = open('myfile.txt', 'w') #open the file named records.txt in write mode
file.write('hello') #write the text 'hello' to the file
file.close() #close the file
myfile.txt contains:
hello
Syntax¶
file = open(fileName, mode) #open the file
file.read() #read all the file
file.readline() #read the next line
file.write(string) #write string to the file
file.close() #close the file
File modes¶
Mode | Description |
r |
|
w |
|
a |
|
Examples¶
Example 1 - Write text to a file¶
output = 'This is some text'
file = open('myfile.txt', 'w')
file.write(output)
file.close()
myfile.txt contains:
This is some text
Example 2 - Use concatenate before writing¶
output = 'This is some text'
output = output + '\nAnd this is some more text'
file = open('myfile.txt', 'w')
file.write(output)
file.close()
myfile.txt contains:
This is some text
And this is some more text
Note
Notice how the \n
escape character is required to make a new line in a text file.
Example 3 - Write a list to a file¶
shoppingList = ['bread', 'jam', 'butter']
file = open('myfile.txt', 'w')
for item in shoppingList:
file.write(item + '\n')
file.close()
myfile.txt contains:
bread
jam
butter
Example 4 - Read entire contents of a file¶
file = open('myfile.txt', 'r')
output = file.read()
print(output)
file.close()
Outputs the file contents onto the screen. If the file contains ‘bread, jam, butter’ with each word on a new line then the output will be:
bread
jam
butter
Example 5 - Read each line from a file¶
file = open('myfile.txt', 'r')
for line in file:
print(line)
file.close()
Outputs the file contents onto the screen. If the file contains ‘bread, jam, butter’ with each word on a new line then the output will be:
bread
jam
butter
Example 6 - Read each line into a list¶
file = open('myfile.txt', 'r')
lines = []
for line in file:
lines.append(line[:len(line)-1])
file.close()
print(lines)
Outputs the file contents onto the screen. If the file contains ‘bread, jam, butter’ with each word on a new line then the output will be:
['bread', 'jam', 'butter']
Example 7 - Catching errors when reading files¶
try:
file = open('myfile.txt', 'r')
output = file.read()
print(output)
file.close()
except IOError as e:
print('Cannot open file. Please check the file exists and is closed.')
Outputs the file contents onto the screen. If the file is missing or cannot be opened then the following is displayed from the exception:
Cannot open file. Please check it is closed.
Key points¶
Note
Remember that \n
can be used to put new lines into files.
Warning
When you write to files be careful as this can easily delete all the contents of the file and there is no way to undo this.
Note
in the statement file = open('myfile.txt', 'r')
, file
is an identifier that refers to the file we are using (you could give it a different name). It is referred to as the file handler.