Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

TK2053

PROGRAMMING PARADIGM
Script Programming with Python

- Text files
Text Files
File is a sequence of bytes stored under a filename
• Text file can be created with any word processor

• Python program can access the values

• We read from a file into memory and write from memory


to a file
Text Files – Open File
Before reading or writing, to open a file

fileobj = open ( filename, mode )

 fileobj is the file object returned by open()


 filename is the string name of the file
 mode is a string indicating the file’s type and what you want to do with it
Text Files – Open File
Before reading or writing, to open a file

fileobj = open ( filename, mode )

The first letter of mode indicates the operation:


 r : read.
 w : write. If the file doesn’t exist, it’s created. If the file does exist, it’s overwritten.
 x : write, but only if the file does not already exist.
 a : append (write after the end) if the file exists.

The second letter of mode is the file’s type:


 t (or nothing) means text.
 b means binary.
Text Files – Open File
Example:

fout = open(‘text.txt', 'wt')

This would tell Python to open a file named text.txt.


The mode tells Python that you intent to write to the text file.
Text Files – Write
1
2
3
4
5

1 Creat a new file named text.txt.


2 Assignment statement, assigned text to the variable t
3 Call write() function to write the value in variable t to the text file
4 write() function returns the number of bytes written
5 Close the file
Text Files – Read
Can read a text file with read(), readline() or readlines()

• Read the entire file at once


Text Files – Read
Can read a text file with read(), readline() or readlines()

• Read the file a line at a time


Text Files – Read
Can read a text file with read(), readline() or readlines()

• Read the file a line at a time

*the easiest way to read a text file is by using an iterator


Text Files – Read
Can read a text file with read(), readline() or readlines()

• To read the file a line at a time, and returns a list of one-line strings
Text Files – Close File
• If files are not closed after opened, it will be closed by
Python after it’s no longer referenced
• However, files should be closed after execution to force any
remaining write to be completed

• Python has context managers to clean up things such as


open files. Use with expression as variable

After the block of code under the context manager, the file is closed automatically.
Text Files – Altering Items in a Text File
• Altering, inserting, or deleting a line of a text file
• Cannot be made directly

• New file must be created


• Read each item from the original file
• Record it, with changes, into new file
• Old file is then erased
• New file is renamed with name of the original

* Look into seek() and tell() to keep track of where you are in the file
Text Files – Altering Items in a Text File
• Functions needed for these tasks must be imported from
standard library module os

• To remove a file

• To rename a file
Hands-on Activities
Open a blank file in your text editor and copy the following text into the file.

In Python you can store as much information as you want.


In Python you can connect pieces of information.
In Python you can model real-world situations.

• Save the file as learning_python.txt.


• Write a program that reads the file and prints what you wrote three times.
• Print the contents once by reading in the entire file, once by looping over the
file object, and once by storing the lines in a list and then working with them
outside the with block.

You might also like