Python Opening and Closing Files

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will embark on an adventure into an ancient temple guarded by a mysterious gatekeeper. Your goal is to uncover the secrets hidden within the temple by mastering the art of opening and closing files in Python.

You find yourself standing in front of the ancient Temple of Python, shrouded in mystery and centuries-old secrets. A gatekeeper, known for his wise and enigmatic demeanor, challenges you to prove your worth by mastering the art of opening and closing files in Python. Only then will the doors of knowledge be unlocked for you within the sacred temple.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python/FileHandlingGroup -.-> python/file_opening_closing("`Opening and Closing Files`") subgraph Lab Skills python/file_opening_closing -.-> lab-271549{{"`Python Opening and Closing Files`"}} end

Entering the Temple

In this step, you will demonstrate your courage and determination by creating a Python script to open and read a file within the temple walls.

  1. In the terminal, navigate to the /home/labex/project directory.
  2. Open a Python script file named open_file.py.
  3. Write the following code in the open_file.py file:
## open_file.py
file_path = '/home/labex/project/sacred_scroll.txt'

with open(file_path, 'r') as file:
    content = file.read()
    print(content)

Run the script:

python open_file.py

The information below should be displayed on your terminal:

The ancient prophecy foretells the chosen one who shall bring balance to the realms.

Unveiling the Secrets

In this step, you will exhibit your newfound knowledge by creating a Python script to write and close a file, thus preserving the wisdom within the temple.

  1. Open a Python script file named write_file.py in same directory.
  2. Write the following code in the write_file.py file:
## write_file.py
file_path = '/home/labex/project/ancient_wisdom.txt'

with open(file_path, 'w') as file:
    wisdom = "The path to enlightenment begins with Python."
    file.write(wisdom)
    print("Ancient wisdom has been inscribed.")
    ## The file is automatically closed at the end of the with statement block.

Run the script:

python write_file.py

The information below should be displayed on your terminal:

Ancient wisdom has been inscribed.

At the same time, you will see a file named ancient_wisdom.txt, the content is: The path to enlightenment begins with Python.

Closing the Temple Gates

In this step, you will demonstrate your respect for the ancient traditions by gracefully closing the files within the temple.

Update the following code at the end of the write_file.py script:

## write_file.py
file_path = '/home/labex/project/ancient_wisdom.txt'

file = open(file_path, 'w')
wisdom = "The path to enlightenment begins with Python."
file.write(wisdom)
print("Ancient wisdom has been inscribed.")
file.close()
## After opening a file with the open() function, the close() method should always be called to close the file in order to ensure that the resources are released correctly.

Run the script:

python write_file.py

The information below should be displayed on your terminal:

Ancient wisdom has been inscribed.

Summary

In this lab, you delved into the realm of file manipulation in Python. By mastering the opening, reading, writing, and closing of files, you have taken the first step in uncovering the ancient wisdom of Python. Embrace this knowledge and let it guide you on your journey towards enlightenment.

Other Python Tutorials you may like