Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Specify the name of the input text file
- input_file_name = "example.txt"
- # Specify the line number you want to extract (4 for the 5th line)
- line_number_to_extract = 4
- # Initialize a variable to store the extracted line
- extracted_line = None
- # Open the text file for reading
- with open(input_file_name, "r") as file:
- # Iterate over the lines in the file
- for current_line_number, line in enumerate(file):
- # Check if the current line number matches the line to extract
- if current_line_number == line_number_to_extract:
- extracted_line = line.strip() # Remove leading/trailing whitespace
- # Check if the line was found and extracted
- if extracted_line is not None:
- print(f"Extracted line {line_number_to_extract + 1}: {extracted_line}")
- else:
- print(f"Line {line_number_to_extract + 1} not found in the file.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement