197k views
5 votes
In Python

1. Write a program that prompts the user for an input file name, reads a collection of integer values from a text file, and prints them to the screen.
2. Write a program that prompts the user for an input file name, reads a collection of integer values from a text file, and determines how many of the values are negative. Assume the values are stored in the file one per line.

User Tolure
by
7.8k points

1 Answer

2 votes

Answer:

1.filename = input("Enter the input file name: ")

with open(filename, 'r') as file:

for line in file:

print(int(line.strip()))

2.filename = input("Enter the filename: ")

try:

with open(filename) as f:

count = 0

for line in f:

if int(line) < 0:

count += 1

print("Number of negative values in the file:", count)

except FileNotFoundError:

print("Error: File not found.")

Step-by-step explanation:

This are my versions of the code in Python hope it helps

User DCR
by
9.5k points