Question

In: Computer Science

The code in this question shows three ways that one can read data from a file...

The code in this question shows three ways that one can read data from a file in a Python program.

Answer the following questions:

  • Using the code as an example, explain why Python is a fixed format programming language.
  • Describe what would be output for each of the print statements in the code listing.
  • Given the difference in output, explain the different roles of readLine(), readLines(), and read() functions.

with open("rainfall.txt","r") as inFile:

   aLine = inFile.readLine()

with open("rainfall.txt", "r") as inFile:

  lineList = inFile.readLines()

with open("rainfall.txt", "r") as inFile:

  fileString = inFile.read())

print(aLine)

print(lineList)

print(fileString)

Solutions

Expert Solution

Note - Since i dont have access to rainfall.txt file. I will explain you the roles of readLine(), readLines(), and read() functions though my text file-test.txt

Consider below text file -

(1) Now if use read() function here -

filepath='C:\\Users\\Preeti\\Desktop\\test.txt'

with open(filepath,"r") as inFile:
fileString = inFile.read()


print(fileString)

Output :

I am LINE 1
I am LINE 2
I am LINE 3
END OF LINES

Explanation - read() function basically reads the entire file and returns it if no size is mentioned.It accepts size as parameter i.e. read(size) . if size is mentioned then it returns quantity of data equal to size.Look at below example where i have given size as 15 -

filepath='C:\\Users\\Preeti\\Desktop\\test.txt'

with open(filepath,"r") as inFile:
fileString = inFile.read(15)


print(fileString)

Output :

I am LINE 1
I a

(2) if we use readline() function -

filepath='C:\\Users\\Preeti\\Desktop\\test.txt'

with open(filepath,"r") as inFile:
aLine = inFile.readline()

print(aLine)

Output :

I am LINE 1

Explanation - readline() function reads single line from the file. So only line 1 is is read and displayed. if you wish to read next line using readline() function , then use readline() function again as shown below

filepath='C:\\Users\\Preeti\\Desktop\\test.txt'

with open(filepath,"r") as inFile:
aLine = inFile.readline()
bLine = inFile.readline()

print(aLine)
print(bLine)

Output :

I am LINE 1

I am LINE 2

(3) if we use readlines() function -

filepath='C:\\Users\\Preeti\\Desktop\\test.txt'

with open(filepath,"r") as inFile:
lineList = inFile.readlines()


print(lineList)

Output :

['I am LINE 1\n', 'I am LINE 2\n', 'I am LINE 3\n', 'END OF LINES']

Explanation - readlines() function returns all the lines in a file in the list format where each element in the list is line from the file.


Related Solutions

Java Code Question: The program is supposed to read a file and then do a little...
Java Code Question: The program is supposed to read a file and then do a little formatting and produce a new txt file. I have that functionality down. My problem is that I also need to get my program to correctly identify if a file is empty, but so far I've been unable to. Here is my program in full: import java.io.*; import java.util.Scanner; public class H1_43 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter...
Using JAVA The following code is able to read integers from a file that is called...
Using JAVA The following code is able to read integers from a file that is called "start.ppm" onto a 3d array called "startImage". Implement the code by being able to read from another file (make up any file name) and save the data onto another 3d array lets say you call that array "finalImage". The purpose of this will be to add both arrays and then get the average Save the average onto a separte 3darray,lets say you call it...
Implement an application that will read data from an input file, interpret the data, and generate...
Implement an application that will read data from an input file, interpret the data, and generate the output to the screen. - The application will have two classes, Rectangle and Lab2ADriver. - Name your Eclipse project, Lab2A. Implement the Rectangle class with the following description. Rectangle Data fields -numRows: int -numCols: int -filled: Boolean Store the number of rows in the Rectangle Store the number of columns in the Rectangle Will define either a filled or unfilled rectangle True =...
Write a program that creates a concordance. There will be two ways to create a concordance. The first requires a document to be read from an input file, and the concordance data is written to an output file.
Concepts tested by this program            Hash Table,            Link List,hash code, buckets/chaining,exception handling, read/write files (FileChooser)A concordance lists every word that occurs in a document in alphabetical order, and for each word it gives the line number of every line in the document where the word occurs.Write a program that creates a concordance. There will be two ways to create a concordance. The first requires a document to be read from an input file, and the concordance data is written to...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
The table shows a short excerpt from the "car weight and mileage" data file on the...
The table shows a short excerpt from the "car weight and mileage" data file on the text CD. That file lists several 2004 model cars with automatic transmission and their x = weight (in pounds) and y = mileage (miles per gallon of gas). The scatterplot is roughly linear and r = -0.74. The regression equation is  = 47.140 - 0.0051x. Automobile Brand Weight Mileage Honda Accord Sedan LX 3137 35 Toyota Corolla 2583 40 Dodge Dakota Club Cab 3869 20...
QUESTION : Read from a file that contains a paragraph of words. Put all the words...
QUESTION : Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line of the input file will contain a sentence with...
C++ question: When cleaning a file the existing data is often read by character. Once a...
C++ question: When cleaning a file the existing data is often read by character. Once a file has been cleaned it is rarely read in by character. Explain Why?
Write a java program that can create, read, and append a file. Assume that all data...
Write a java program that can create, read, and append a file. Assume that all data written to the file are string type. One driver class and a class that contains the methods. The program should have three methods as follows: CreateFile - only creating a file. Once it create a file successfully, it notifies to the user that it creates a file successfully. ReadingFile - It reads a contents of the file. This method only allow to read a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT