Knowee
Questions
Features
Study Tools

Write a Python program to read a file line by line store it into an array.

Question

Write a Python program to read a file line by line store it into an array.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple Python program that reads a file line by line and stores it into an array:

# Open the file in read mode
file = open('filename.txt', 'r')

# Initialize an empty list to store the lines
lines = []

# Loop through each line in the file
for line in file:
    # Add the line to the list
    lines.append(line.strip())

# Close the file
file.close()

# Now, 'lines' is an array that contains each line in the file as an element
print(lines)

In this program, replace 'filename.txt' with the path to your file. The strip() function is used to remove the newline character at the end of each line. If you want to keep the newline character, you can remove .strip().

This problem has been solved

Similar Questions

Copy contents of one file to another fileWrite a Python program that copies the content from one file to another file.Note: Take the input file name from the user.Aim:To create a Python program that reads a text file, copies its contents to another file, and prints the contents to the console.Algorithm:Step 1: Accept a filename input from the user and store it in the variable file.Step 2: Open the input file (file) in read mode and the output file ('OutputData3.txt') in write mode using the open() function.Step 3: Iterate through each line in the input file (fin).Write each line to the output file (fout).Step 4: Close both the input and output files using the close() method.Step 5: Open the output file ('OutputData3.txt') in read mode and store it in the variable fin.Step 6: Iterate through each line in the output file (fin).Print each line to the console.Step 7: Close the output file (fin).Step 8: End the program.Sample Test CasesTest Case 1:Expected Output:Enter·file·name:·InputData1.txtThis·is·this.That·is·this.·That·is·that.·This·is·That.Now·tell·me·what·is·that?And·what·is·this?Confused?Haha!!Test Case 2:Expected Output:Enter·file·name:·InputData2.txtIn·a·coastal·town,·a·lighthouse·stood·tall,holding·a·magical·secret.·The·lighthousewhispered·stories·to·the·sea,·guidinglost·ships·and·providing·comfort·toweary·sailors.

Write a function in Python to read a text file, Alpha.txt and displaysthose lines which begin with the word ‘You’

Define a function named read_length_per_line(filename) that takes a filename as a parameter and returns a list of integers. The function reads the contents of the file specified in the parameter and returns a list of values where each value is the length per line. For example, if a file contains the following:Hans loved to make up storiesHe would sit for hours in the tiny yardthen, the function returns [29, 39] where 29 is the number of characters in the first line and 39 is the number of characters in the second line.Here are some text files you can test your code with:names.txtmini_names.txtFor example:Test Resultprint(read_length_per_line('sentences.txt'))[29, 39]

Consider the following text file: Example1.txt:This is line 1This is line 2This is line 3What is the output of the following lines of code?123456 with open("Example1.txt","r") as file1:      FileContent=file1.read()        print(FileContent)

python programs to create, read, write, append and delete a file

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.