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!")