0% found this document useful (0 votes)
5 views1 page

Python Code 4 Questions

The document provides Python programming exercises with code examples for four tasks: reading a file, copying files using the shutil module, demonstrating assertions, and logging messages of different severity levels. Each task includes a brief explanation and sample code. These exercises are designed to practice basic file handling, error checking, and logging in Python.

Uploaded by

madivalshridhar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Python Code 4 Questions

The document provides Python programming exercises with code examples for four tasks: reading a file, copying files using the shutil module, demonstrating assertions, and logging messages of different severity levels. Each task includes a brief explanation and sample code. These exercises are designed to practice basic file handling, error checking, and logging in Python.

Uploaded by

madivalshridhar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python Programming Practice - Code for 4 Questions

1. Write a program to read the contents of a file and display it.


# File Reading Example
file = open("sample.txt", "r")
content = file.read()
print("File content:\n", content)
file.close()

2. Write a program to copy files using shutil module.


import shutil

# Copying a file
shutil.copy("sample.txt", "copy_sample.txt")
print("File copied successfully!")

3. Write a program to demonstrate assertion.


# Assertion Example
marks = 75
assert marks >= 0 and marks <= 100, "Marks should be between 0 and 100"
print("Valid marks:", marks)

4. Write a program to log messages of different severity levels.


import logging

# Configure logging
logging.basicConfig(level=logging.DEBUG)

logging.debug("Debugging in progress")
logging.info("Informational message")
logging.warning("This is a warning")
logging.error("An error has occurred")
logging.critical("Critical issue!")

You might also like