Question: This is Data System question using python Jupyter Notebook. Can anyone please help me answering these questions? --> These are the packages that need to
This is Data System question using python Jupyter Notebook.
Can anyone please help me answering these questions?
--> These are the packages that need to be imported.
import os import os.path import io import sys from contextlib import redirect_stdout
datadir = "publicdata"
Q 1: Write a function that takes a single argument of a file path and, by opening, reading and closing the file, obtains the count of characters in the file. If the path argument does not refer to an existing file, the function should return -1.
fileCharCount(path)
-->
raise NotImplementedError() return count print("Tennyson count:", fileCharCount(os.path.join(datadir, "tennyson.txt")))
--> The answer should work like
assert fileCharCount(os.path.join(datadir, "tennyson.txt")) == 124 assert fileCharCount(os.path.join(datadir, "hello.txt")) == 57 assert fileCharCount(os.path.join(datadir, "twolines.txt")) == 25 assert fileCharCount(os.path.join(datadir, "foobar.txt")) == -1
Q2. Suppose, in the data directory given by python variable `datadir`, there is a file named `tennyson.txt`. The content of the file consists of the five lines as shown here:
Twilight and evening bell, And after that the dark!
And may there be no sadness of farewell, When I embark;
So there are two lines, an empty line, and then two following lines. Each of these five lines is terminated by the newline (`' '`). Some lines begin immediately with a non-space character, and some lines begin with one or more spaces. Although not visible in the above, some of the lines have space characters at the end, before the terminating newline.
For the `tennyson.txt` file, we want to read the lines and return a list of strings, with one entry in the list for each line of the file. Use repeated invocations of the `readline()` method in your solution, and, because this is specific to the `tennyson.txt` file, your function can use the knowledge that there are five lines in the file.
tennysonLines()
-->
raise NotImplementedError() print(tennysonLines())
--> The answer should work like
filelines = tennysonLines()
assert len(filelines) == 5 assert filelines[0] == 'Twilight and evening bell, ' assert filelines[-1] == ' When I embark; ' assert filelines[2] == ' '
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
