Here is a typical example of processing data from a file. Suppose you are given a text file that contains a sequence of floating-point values, stored one value per line. You need to read the values and write them to a new output file, aligned in a column and followed by their total and average value. Look at the tab below for input.txt file which has 5 values. Sample of the input and formatted output is shown here on the image.
Sample Input & Formatted output
Update the code so that the data.txt file is read in and then written back to the results.txt file with a line number inserted beside each line. You can see an example of the output in the results.txt tab below.
Sample Input & Formatted output
There are two methods for reading an entire file. The call inputFile.read() returns a string with all characters in the file. The readlines method reads the entire contents of a text file into a list:
Each element in the list returned by the readlines method is a string containing a single line from the file (including the newline character). Once the contents of the file are in the list, you can access lines in the list by position, as in listOfLines[2]. You can also iterate over the entire list:
These methods are very useful when you need to load the contents of a small file. However, you should avoid using them for large files because they can require a large amount of memory to store all of the strings.
file's entire contents as a string
next character from file as a string
next line from file as a string
file's contents as a list of lines
Run the following program which shows how data from a file can be summarised. Then modify the program so that it can read in the testdata.txt file which also contains the surname of the employee. In this new version of the program, print out the name and surname of the employee so it looks like the following.
Suzy Brown ID 123 worked 31.50 hours: 6.30 / day
Brad Notts ID 456 worked 36.80 hours: 7.36 / day
Jenn Santana ID 789 worked 39.50 hours: 7.90 / day
Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
Opens a file for both reading and writing. The file pointer will be at the beginning of the file.
Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.
Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.