Title here
Summary here
Answer :
file_name = input("Please enter the file name: ")
try:
file = open(file_name, 'r')
for line in file:
if '#' in line:
print(line.strip())
except FileNotFoundError:
print(f"Error: The file '{file_name}' was not found.")
finally:
# Ensuring file is closed
if 'file' in locals():
file.close()
with
handles the file opening and closing automatically, ensuring that the file is always closed, even if an error occurs within the block.
file_name = input("Please enter the file name: ")
try:
with open(file_name, 'r') as file:
for line in file:
if '#' in line:
print(f"Line : {line.strip()}"
except FileNotFoundError:
print(f"Error: The file '{file_name}' was not found.")
Answer :
file_name = input("Please enter the file name: ")
se_pattern = input("Enter Pattern to search: ")
try:
file = open(file_name, 'r')
for line in file:
if se_pattern in line:
print(line.strip())
except FileNotFoundError:
print(f"Error: The file '{file_name}' was not found.")
finally:
if 'file' in locals():
file.close()
Answer :
file_name = input("Enter file name to read from: ")
output_file_name = input("Enter file name to copy contents into: ")
try:
file = open(file_name, 'r')
output_file = open(output_file_name, 'w')
num_lines = 0
num_words = 0
num_characters = 0
for line in file:
num_lines += 1
num_characters += len(line)
# length of line is character count
num_words += len(line.split())
# Split by spaces and count the words
# Write the line to the output file
output_file.write(line)
print(f"Number of lines: {num_lines}")
print(f"Number of words: {num_words}")
print(f"Number of characters: {num_characters}")
except FileNotFoundError:
print(f"Error: The file '{file_name}' was not found.")
finally:
if 'file' in locals():
file.close()
if 'output_file' in locals():
output_file.close()
Same but with with - as
to handle file open and close.
file_name = input("Enter file name to read from: ")
output_file_name = input("Enter file name to copy contents into: ")
try:
with open(file_name, 'r') as file:
num_lines = 0
num_words = 0
num_characters = 0
with open(output_file_name, 'w') as output_file:
for line in file:
num_lines += 1
num_characters += len(line)
num_words += len(line.split())
output_file.write(line)
print(f"Number of lines: {num_lines}")
print(f"Number of words: {num_words}")
print(f"Number of characters: {num_characters}")
except FileNotFoundError:
print(f"Error: The file '{file_name}' was not found.")
Answer :
file_name = input("File name to read from: ")
positive_file_name = input("File to store positive numbers: ")
negative_file_name = input("File to store negative numbers: ")
try:
file = open(file_name, 'r')
positive_numbers = []
negative_numbers = []
for line in file:
for word in line.split():
try:
num = float(word)
if num >= 0:
positive_numbers.append(num)
else:
negative_numbers.append(num)
except ValueError:
# Skip words that are not numbers
continue
file.close()
with open(positive_file_name, 'w') as pos_file:
for num in positive_numbers:
pos_file.write(f"{num}\n")
with open(negative_file_name, 'w') as neg_file:
for num in negative_numbers:
neg_file.write(f"{num}\n")
except FileNotFoundError:
print(f"Error: The file '{file_name}' was not found.")
Answer :
# Ask for the input file name
file_name = input("Please enter the file name to read from: ")
output_file_name = input("Please enter the file name to store the sorted lines: ")
try:
file = open(file_name, 'r')
# Read all lines from the file into a list
lines = file.readlines()
# Sort the lines
lines.sort()
# Print the sorted lines
for line in lines:
print(line.strip())
# Open the output file for writing
output_file = open(output_file_name, 'w')
for line in lines:
output_file.write(line)
file.close()
output_file.close()
print(f"\nThe sorted lines have been written to '{output_file_name}'.")
except FileNotFoundError:
print(f"Error: The file '{file_name}' was not found.")
Answer :
# Ask for the input file name
file_name = input("Please enter the file name to read from: ")
output_file_name = input("Please enter the file name to store the sorted lines with line numbers: ")
try:
file = open(file_name, 'r')
lines = file.readlines()
# Sort the lines in reverse order
lines.sort(reverse=True)
output_file = open(output_file_name, 'w')
line_num = 1
for line in lines:
output_file.write(f"Line {line_num}: {line}")
line_num += 1
file.close()
output_file.close()
print(f"The sorted lines with line numbers have been written to '{output_file_name}'.")
except FileNotFoundError:
print(f"Error: The file '{file_name}' was not found.")
Answer :
file_name = input("Please enter the file name to read from: ")
try:
file = open(file_name, 'r')
lines = file.readlines()
print("First 5 lines:")
for i in range(min(5, len(lines))):
# To prevent going out bound if len < 5
print(lines[i].strip())
# Display the last 5 lines
print("\nLast 5 lines:")
for i in range(max(0, len(lines)-5), len(lines)): # Ensuring we don't go out of bounds
print(lines[i].strip())
file.close()
except FileNotFoundError:
print(f"Error: The file '{file_name}' was not found.")
Consider the following file num_pairs.txt
, read data and find the line total and write the line as well as total to a new file.
num_pairs.txt
1.3 3.4
2 4.2
-1 1
Answer :
# Ask for the input and output file names
input_file_name = "num_pairs.txt"
output_file_name = "output_with_totals.txt"
try:
with open(input_file_name, 'r') as file:
lines = file.readlines()
with open(output_file_name, 'w') as output_file:
for line in lines:
numbers = line.split()
total = sum(float(num) for num in numbers)
output_file.write(f"{line.strip()} = {total}\n")
print(f"The file '{output_file_name}' has been created with line totals.")
except FileNotFoundError:
print(f"Error: The file '{input_file_name}' was not found.")
except ValueError:
print("Error: There was a problem with converting data to numbers.")
# Get two numbers from the user
n1, n2 = eval(input()) # 1
# Compute sum of the two numbers
print(n1 + n2) # 2
# Compute average of the two numbers
print(n1+n2/2) # 3 Division happens first
# Compute a quotient
print(n1/d1) # 4 d1 not declared
# Compute a product
n1*n2 = d1 # 5 Reversed experssion assignement
For each line listed in the comments, indicate whether or not an interpreter error, run-time exception, or logic error is present. Not all lines contain an error.