import random
# Open the file in write mode
with open("numbers.txt", "w") as file:
# Loop to generate 10 random numbers
for _ in range(10):
# Generate a random number between 1 and 100
random_number = random.randint(1, 100)
# Write the random number to the file, followed by a newline character
file.write(str(random_number) + "\n")
def calculate_total_from_file(filename):
"""Calculates the total of the numbers in a file.
Args:
filename: The name of the file containing the numbers.
Returns:
The total of the numbers in the file, or None if an error occurs.
"""
total = 0
try:
with open(filename, "r") as file:
for line in file:
try:
# Convert the line to a number and add it to the total
total += float(line.strip())
except ValueError:
# Skip lines that contain non-numeric characters
pass
except FileNotFoundError:
# Handle the case where the file is not found
print(f"Error: File '{filename}' not found.")
return None
return total
if __name__ == "__main__":
filename = "numbers.txt"
total = calculate_total_from_file(filename)
if total is not None:
print(f"The total of the numbers in '{filename}' is: {total}")
def count_digits_in_file(filename):
"""Counts the total number of digits in a file.
Args:
filename: The name of the file to read.
Returns:
The total number of digits in the file, or 0 if the file is empty.
"""
total_digits = 0
try:
with open(filename, "r") as file:
for line in file:
# Count digits in the current line
total_digits += sum(char.isdigit() for char in line)
except FileNotFoundError:
# Handle the case where the file is not found
print(f"Error: File '{filename}' not found.")
return 0
return total_digits
if __name__ == "__main__":
filename = "sample.txt"
total_digits = count_digits_in_file(filename)
print(f"The total number of digits in '{filename}' is: {total_digits}")
def display_vowel_lines(filename):
"""Displays lines from a file that begin with a vowel.
Args:
filename: The name of the file to read.
"""
vowels = "aeiouAEIOU"
try:
with open(filename, "r") as file:
for line in file:
# Check if the line starts with a vowel (case-insensitive)
if line.strip()[0] in vowels:
print(line.strip())
except FileNotFoundError:
# Handle the case where the file is not found
print(f"Error: File '{filename}' not found.")
if __name__ == "__main__":
filename = "zen.txt"
display_vowel_lines(filename)
halaaaaaa super thank you pooooo1.
Code:import random # Open the file in write mode with open("numbers.txt", "w") as file: # Loop to generate 10 random numbers for _ in range(10): # Generate a random number between 1 and 100 random_number = random.randint(1, 100) # Write the random number to the file, followed by a newline character file.write(str(random_number) + "\n")
2.
Code:def calculate_total_from_file(filename): """Calculates the total of the numbers in a file. Args: filename: The name of the file containing the numbers. Returns: The total of the numbers in the file, or None if an error occurs. """ total = 0 try: with open(filename, "r") as file: for line in file: try: # Convert the line to a number and add it to the total total += float(line.strip()) except ValueError: # Skip lines that contain non-numeric characters pass except FileNotFoundError: # Handle the case where the file is not found print(f"Error: File '{filename}' not found.") return None return total if __name__ == "__main__": filename = "numbers.txt" total = calculate_total_from_file(filename) if total is not None: print(f"The total of the numbers in '{filename}' is: {total}")
3.
Code:def count_digits_in_file(filename): """Counts the total number of digits in a file. Args: filename: The name of the file to read. Returns: The total number of digits in the file, or 0 if the file is empty. """ total_digits = 0 try: with open(filename, "r") as file: for line in file: # Count digits in the current line total_digits += sum(char.isdigit() for char in line) except FileNotFoundError: # Handle the case where the file is not found print(f"Error: File '{filename}' not found.") return 0 return total_digits if __name__ == "__main__": filename = "sample.txt" total_digits = count_digits_in_file(filename) print(f"The total number of digits in '{filename}' is: {total_digits}")
4.
Code:def display_vowel_lines(filename): """Displays lines from a file that begin with a vowel. Args: filename: The name of the file to read. """ vowels = "aeiouAEIOU" try: with open(filename, "r") as file: for line in file: # Check if the line starts with a vowel (case-insensitive) if line.strip()[0] in vowels: print(line.strip()) except FileNotFoundError: # Handle the case where the file is not found print(f"Error: File '{filename}' not found.") if __name__ == "__main__": filename = "zen.txt" display_vowel_lines(filename)

