0% found this document useful (0 votes)
22 views

10.3 Files

This document discusses file input/output operations in Python such as creating and opening files, writing, reading, appending, and deleting data from files. It provides code examples for opening files in different modes, writing, reading, and manipulating file contents. Functions covered include reading specific lines, renaming,

Uploaded by

steph
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

10.3 Files

This document discusses file input/output operations in Python such as creating and opening files, writing, reading, appending, and deleting data from files. It provides code examples for opening files in different modes, writing, reading, and manipulating file contents. Functions covered include reading specific lines, renaming,

Uploaded by

steph
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

10 Data types and structures

10.3 Files

Compiled by Linda Mzemba - 2023 References: pythontutorial.net,


[email protected] tutorialspoint.com
Objectives
• Create a new text file
• Write data to a file
• Read data in a file
• Append data to a file
• Delete data from a file
Access modes
File access mode Pseudocode Python Syntax Description
Write only WRITE ‘w’ Opens a file for writing. If the file
doesn’t exist, it is created.
Read only READ ‘r’ Opens a file for reading, if the file
does not exist it will show an
error.
Write and Read READ ‘w+’ Opens a file for both writing and
reading
Append only APPEND ‘a’ Opens a file for writing. If the file
doesn’t exist, it is created.
Create a text file

Pseudocode Python code


OPEN <filename> FOR <file mode>

e.g. OPEN example.txt FOR WRITE


Write data to file

Pseudocode Python
OPEN <file identifier> FOR WRITE
WRITEFILE <file identifier>, <variable>

e.g.
myFile = ‘example.txt’
lineText = ‘Hello world’
WRITEFILE myFile, lineText
Append data to file

Pseudocode Python
OPEN <file identifier> FOR WRITE
WRITEFILE <file identifier>, <variable>

e.g.
myFile = ‘example.txt’
OUTPUT “Enter your name”
INPUT lineText
WRITEFILE myFile, lineText
Read data in a file

Pseudocode Python
OPEN <file identifier> FOR READ
READFILE <file identifier>

e.g.
.read – displays all the contents
myFile = ‘example.txt’
READFILE myFile .readline – shows one line at a
time
Read data in a file

With statement Python


The with statement automatically closes the
file.
Read data in a file as a list of strings

Python Python

• To remove the blank line use the


.strip() method
• E.g. print(line.strip())
Read a specific line(s) in a file

Python Python

[N] Prints the Nth line


[N:] Prints from the Nth line to the end of the file
[0:N] Prints from the first to the Nth line
[-N] Prints the Nth line from the end of the file
[-N:] Prints the last N lines

TRY THIS: print the last line of the file.


Rename a file

Python Python
• Rename the file example.txt (the
src) to question.txt (the dst)
• If the src file does not exist or
the or the dst file already exists
the os.rename() function raises a
FileNotFound error
Avoiding error when renaming a file

Python
• try...except Statement
Delete a file

Python
• If the file does not exist an
error will be generated.
• What can you do to overcome
this?
Delete a file – check if file exists

Python
• Check if the file exists
• If it does not then no error will
be generated
Delete a specific line from a file
Practice
1. Create an new text file called schools.txt.
2. Create a procedure that allows a user to add a new school name to
the text file.
3. Create a procedure that reads the file and displays all the school
names in the file.
4. Create a function that searches for a school name and returns True
if the school name exists and False if the school name does not
exist in the text file.
5. Create a function that allows a user to enter the school name they
want to delete and displays the edited text file.

You might also like