Overview

Introduction

Welcome to the Copying 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 - Making Copies of Text Files

After completing this lab, you will be able to:

  • Copy the contents of one text file to another 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 10 minutes to complete this lab.

Exercise 1 - Making Copies of Text Files

A file is a named location on a disk to permanently store data in a hard disk. Python recognizes two types of files—text and binary.

Recall that Python has a native file object that makes it easier to work with files. The built-in open() function gets you access to that file object. Once you have the access to the file object, you can collect information about the file you opened and also modify that file.

The open() function takes two parameters—filename and mode (read[‘r’], write[‘w’], append[‘a’], and read-write[‘r+’]). If the mode is not specified, the ‘r’ or read mode is assumed and the file is opened as a read-only file. The syntax of this function is as follows:

file_object = open(“filename”, “mode”)

To be able to copy files, you will need to open the text file to be copied in the read[‘r’] mode and the new file in the write[‘w’] or, append[‘a’] mode.

In this exercise, you will perform two tasks. In the first task, you will download the PrintFile.py program file from the Intranet and open it in Python IDLE environment. In the second task, you will learn to write a program to open the text file sample.txt, modify it to read, and copy its contents to a new text file.

Learning Outcomes

After completing this exercise, you will be able to:

  • Copy the contents of one text file to another in Python programs

Comprehensive Learning

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