Reading Files

Practice Labs Module
Time
6 minutes
Difficulty
Intermediate

Welcome to the Reading Files Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.

Join over 3 million cybersecurity professionals advancing their career
Sign up with
or

Already have an account? Sign In »

Overview

Introduction

Welcome to the Reading Files Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.

Learning Outcomes

In this module, you will complete the following exercise:

  • Exercise 1 - Working with Files

After completing this lab, you will be able to:

  • Open, read, and close text files in Python programs

Exam Objectives

The following exam objectives are covered in this lab:

  • 3.1 Construct and analyze code segments that perform file input and output operations

Lab Duration

It will take approximately 20 minutes to complete this lab.

Exercise 1 - Working with Files

File is a named location on a disk to store related information. It is used to store data in a non-volatile memory permanently; for example, a hard disk.

The concept of files in Python is different from that in Windows. In Windows, for example, a file is any item manipulated, edited, or created by the user or the operating system. There are many types of files in Windows: text, image, music, video, and executable etc.

Python, however, recognizes only two types of files—text and binary. Here is the difference:

  • Text files are simple text structured as a sequence of lines, where each line includes a sequence of characters that is terminated by the end of line (EOL) character. There are several types of EOLs such as the comma {,} a backslash {/}, or a newline character {\n}. An EOL ends the current line and tells the interpreter that a new one has begun.
  • Binary files contain binary data, which is only readable by a computer.

‘r’ - Read mode, which allows the file to be read-only; you can’t edit a file in this mode

‘w’ - Write mode, which is used to edit and write new information to the file; note that any existing files with the same name will be erased when this mode is activated

‘a’ - Append mode, which is used to add new data to the end of the file

‘r+’ - Special read and write mode, which allows reading and writing to the file

If the mode is not specified, the ‘r’ or read mode is assumed, and the file is opened as a read-only file.

In this exercise, you will perform two tasks. In the first task, you will create a text file using Notepad. In the second task, you will learn to write a program to open the same text file created in task 1 and print its contents in upper case.

Learning Outcomes

After completing this exercise, you will be able to:

  • Open, read, and close text files in Python programs
Learning Partner
Comprehensive Learning

See the full benefits of our immersive learning experience with interactive courses and guided career paths.