Python Codes
Python Codes
Practice problems
1) Why a python program to write twinkle twinkle little star poem
Def twinkle_verse():
Def sky_verse():
# Calling functions
Twinkle_verse()
Sky_verse()
Twinkle_verse()
3) Write a python program to print the contents of a directory using OS module. Search
online for the function which does that.
Import os
Try:
# Get the list of all files and directories
Entries = os.listdir(directory_path)
Print(f”Contents of ‘{directory_path}’:”)
For entry in entries:
Print(entry)
Except FileNotFoundError:
Print(f”The directory ‘{directory_path}’ does not exist.”)
Except PermissionError:
Print(f”Permission denied to access ‘{directory_path}’.”)
Except Exception as e:
Print(f”An error occurred: {e}”)
Try:
# Get the list of all files and directories in the specified path
Entries = os.listdir(directory_path)