1 of 30

Topic 4: Files

Learning Outcomes:

(a) Identify the use of input/output files using CSV and text files

CP125

2 of 30

Topic 4: Files

Table of content

  1. Why use file
  2. Type of files
  3. Working with .txt
    1. Open file
    2. Read
    3. Write
    4. Close
  4. Working with .csv
    • Open file
    • Read
    • Write
    • Close

CP125

3 of 30

  1. Why use Files?

Learning about text files in Python is important because:

​

  • Data Persistent/storage : Data stored in memory will be deleted when the program terminate

​

  • Data Exchange: They're a common format for exchanging data between programs.

​

  • File Handling Skills: Working with text files teaches essential file handling operations.

​

  • Configuration: Many applications use text files for configuration settings.

​

  • Data Processing: Text files are often used as input sources for data processing tasks.

​

  • Compatibility: They're platform-independent and easily transferable between systems and applications.

​

4 of 30

2. Type of File in Python

There are 2 type of files in python

​

  • Text File (our focus) : Text file usually we use to store character data. For example, test.txt

​

  • Binary Files is used to store binary data such as images, video files, audio files, etc.

5 of 30

What is text File ?

​

  • A file that contains human readable character

​

  • No embedded formatting commands

(like bold or italic) or binary data

​

  • Data represented in sequences of (ASCII) character organized into lines

​

  • Typically terminated by new line character (\n)

​

  • Common text file format is as shown Figure 1

Figure 1

.txt

Common

Text File Format

.json

.csv

.yaml

.xml

.yaml

6 of 30

.csv

.txt

  • Pure text document that only contains unformatted or unstructured text.

​

  • File is recognized by any text editing or word processing program.

​

demofile.txt

  • Simplest spreadsheet/database file.

​

  • can be open with spreadsheet software like Excel and Google Sheet

daily_expenses

What is .txt and .csv File

7 of 30

Structure of .csv File

open in excel

open in notepad

  • row is a record
  • column is a field within the record.
  • the first row is a header
  • values is separated by comma, hence the name comma separated value (csv)

Record

Field

Header

8 of 30

3. File Operation for .txt and .csv file

Basic file operations

​

  1. Open File

​

2. Read File

​

3. Write to File

​

4. Close File

Python provides built-in functions and external modules(csv) to support these operations

9 of 30

.txt File

3. Working with .txt file

10 of 30

.txt Python File Operation Functions & Methods

open file

f= open(path_to_file,mode)

read file

f.read() # read entire content of the file as a STRING

f.readline() # read a single line from a file as a STRING

f.readlines()# read all lines in the file and return them as a LIST of STRING

write to file

f.write() # write a single line to a file

f.writelines() # write a list of string to a file

close file

f.close() # close the file

11 of 30

  1. Opening File Syntax

f= open(path_to_file,mode)

‘path/to/txt/file/expenses.txt’

r - read

a - append

w - write

The mode you pick decides what action you can do with the file and behaviour of the file.

​

r+ - read/write

a+ - append/read

w+ - write/read

12 of 30

Opening Mode Description,Action and Behavior

13 of 30

Opening File Syntax in Different File Location

File location

Opening File Syntax

Same folder

f=open(‘demo.txt’,w+)

​

Local File

(e.g. inside Documents)

​

f=open(‘c:/User/user/Documents/project_folder/demo.csv’,w+)

Google Drive (Google Colab)

f=open(‘/content/drive/MyDrive/project_folder/demo.txt’,w+)

Mode ‘w+’ , The file will be created if the file does not exist.

14 of 30

File Does Not Exist Error

Traceback (most recent call last):

File "c:/User/user/Documents/filenotexist.py", line 9, in <module>

f=open('demofiles.txt', 'r')

FileNotFoundError: [Errno 2] No such file or directory: 'demofiles.txt'

Error Output

Opening File Syntax

*ERROR always happened cause by either wrong directory OR file does not exist

15 of 30

2. Reading File Syntax

f=open(path/to/csv/file/expenses.txt,w+)

f.read()

.txt file can be read using the built-in read function

16 of 30

output

Input file : demofile.txt

2(a) Reading Entire content of file

f = open("demofile.text", "r")

text = f.read()

print(f.read())

print(type(text))

​

​

​

Hello! Welcome to demofile.txt

This file is for testing purposes.

Good Luck!

<class 'str'>

17 of 30

output

Input file: demofile.txt

2(b) Reading a Single Line

It reads one line from the file each time it is called. Because it only called one, only one line is read.

18 of 30

output

demofile.txt

2(b) Reading a Single Line (contn’)

It reads one line from the file each time it is called, the program output first and second line as the method readline() is called twice

19 of 30

demofile.txt

output

2(c) Reading all Lines

It reads all line, and output a list of string,

each element represent a line in the file.

20 of 30

2(c)(1.0) Reading all Lines - from google colab

We may come across error if your input TEXT file contains unacceptable encoding style(refer to the error message). Make sure input file contains TEXT only, no funny characters.

File location in colab

21 of 30

2(c)(2.1) Reading all content - from google colab

read(), readline(), readlines()

​

NOTE: return values are string

22 of 30

2(c)(2.2) Reading a line of the content - from google colab

read(), readline(), readlines()

​

NOTE: Return values are string

23 of 30

2(c)(2.3) Reading all content - from google colab

read(),readline(), readlines()

​

Return value is a list.

24 of 30

2(c)(3.0) Reading all Lines - from google colab

We can use “with …. “ as well

  1. mount drive to google colab
  2. paste path directory
  3. read function

25 of 30

The write() method writes a specified text to the file.

Where the specified text will be inserted depends on the open file mode and stream position.

"a": The text will be inserted at the current file stream position, default at the end of the file.

"w": The file will be emptied before the text will be inserted at the current file stream position, default 0.

3. Write File Syntax

26 of 30

3. Write File Syntax

f=open(path/to/csv/file/expenses.txt,w+)

f.write(‘content to write\n’)

or

f.write(‘content to write’+’\n’)

​

\n is added to the end of the string to ensure the next string start at a new line.

27 of 30

3(a) Write a Single String to File

output

  • write method adds a single line to the file.

​

  • you should end the string with \n.

  • which indicate end of one line and the beginning of the next line.

​

  • File is close after the program end.

28 of 30

3(a) Write a Single String to File (cont’)

output

  • Writing a list to a file can also be accomplished using write() method inside a for loop.

​

  • The loop iterates over the list and writes each element to the file

​

  • File is close after the program end.

29 of 30

3(b) Write list of String to File

output

  • A more concise and efficient way is to use writelines() method.

30 of 30

4. Close File

file.close()

​

In Python, it's important to close files after you've finished working with them to release system resources

​