In Provided Python code the first line initializes a variable file_path with the path of the file to be read. You can replace ‘your_file.txt’ with the actual file path.
The with statement opens the file in read mode and assigns it to the variable file. This ensures that the file is closed properly after the block of code is executed.
The for loop reads each line of the file using the file variable and stores it in a list called lines. The strip() method is used to remove any leading or trailing whitespace characters from each line.After the loop completes, lines contains each line of the file as an element.Finally, the print() function is used to display the contents of the lines list.
file_path = 'your_file.txt' # Replace 'your_file.txt' with the actual file path # Read the file line by line and store each line in a list with open(file_path, 'r') as file: lines = [line.strip() for line in file] # Now, 'lines' is a list containing each line of the file as an element print(lines)