Python3 please use while loop to solve this question,specific information is on the graph
Screenshot
Program
#for file path exist check
import os
#Function to get number of line in the file
def get_num_lines(filename):
with open(filename) as fp:
lines=fp.readlines()
fp.close()
return len(lines)
#Function to read file lines if the count of lines less than
50
#which append all lines in the list
def read_file_less50(filename,lst):
with open(filename) as fp:
line=fp.readline().rstrip("\n\r")
lst.append(line)
while line:
line=fp.readline().rstrip("\n\r")
lst.append(line)
fp.close()
#function to get middle 50 lines of a file
def read_file_more50(filename,num,lst):
num=num/2
start=num-25
end=num+25
cnt=0
with open(filename) as fp:
line=fp.readline().rstrip("\n\r")
cnt+=1
while line:
line=fp.readline().rstrip("\n\r")
cnt+=1
if(cnt>=start and cnt<end):
lst.append(line)
fp.close()
#function for getting file data
def read_file_middle50(filename):
try:
#for file lines
lst=[]
#Check file name is a
string
#Otherwise TypeError
exception raise
if(isinstance(filename,str)):
#Check file exist
#Otherwise file not found exception
if(os.path.exists(filename)):
#Get number of line in the given file
num_lines=get_num_lines(filename)
#Check empty file
#return empty list
if(num_lines==0):
return lst
#If line count<50 return all lines
elif(num_lines<50):
read_file_less50(filename,lst)
return lst
#If line count>50 return middle 50 lines
else:
read_file_more50(filename,num_lines,lst)
return lst
#file not found error raise
else:
raise FileNotFoundError(filename+" file not found!!!")
#Not string error
raise
else:
raise TypeError("parameter filename is not a string")
#Exceptions
except TypeError as ex:
print(ex)
except FileNotFoundError as ex:
print(ex)
#Test part
read_file_middle50(10)
read_file_middle50("test.txt")
print(read_file_middle50("C:/Users/deept/Desktop/test.txt"))
--------------------------------------------
output
parameter filename is not a string
test.txt file not found!!!
['24', '25', '26', '27', '28', '30', '31', '32', '33', '34', '35',
'36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46',
'47', '48', '50', '51', '52', '53', '54', '55', '56', '57', '58',
'59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69',
'70', '71', '72', '73', '74', '75']
----------------------------
Program Screenshot
Python3 please use while loop to solve this question,specific information is on the graph Question 2...
Python3, write a program, specific information is in the graphs, I got 7 marks out of 10, you may change my codes to let it gives correct output,restrications are also on the graph, for loop, in key word, enumerate,zip,slices, with key word can't be used. All the information is on the graph, is_shakespeare_play(line) may be a function given that can check whether is Shakespeare play Question 2- 10 marks Write the function keep titles short (filename, max charactars) which takes...
I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the...
create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...
I am having a little trouble with my Python3 code today, I am not sure what I am doing wrong. Here are the instructions: and here is my code: update: I have seen I did not close x in sumFile and I am still only getting a 2/10 on the grader. any help appreciated. Lab-8 For today's lab you going to write six functions. Each function will perform reading and/or write to a file Note: In zybooks much like on...
Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...
C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank you. Use the given function to create the program shown in the sample run. Your program should find the name of the file that is always given after the word filename (see the command line in the sample run), open the file and print to screen the contents. The type of info held in the file is noted by the word after the filename:...
Sample program run - Needs to be coded in python -Enter the text that you want to search for, Or DONE when finished: valiant paris The valiant Paris seeks for his love. -Enter the text that you want to search for, or DONE when finished: Romeo and Juliet Enter Romeo and Juliet aloft, at the WIndow -- Enter the text that you want to search for, DONE when finished: DONE # copy the following two lines into any # program...
use python IDEL Please highlight the answer Problem 2 Files. Since almost all data can be stored in files, knowing how to use files in programs is invaluable. Today you will practice with opening a file for reading, writing and editing using the Python interpreter. Do not type messages in red into Python shell Question 14 0.5 pts create a new file mytext.txt for >>>f1 = open("mytest.txt","w+") writing & reading >>>f1.write("hello\n") >>>for i in range(5): f1.write(str(i) + "\n") >>>f1.readlines() record...
need this in c programming and you can edit the code below. also give me screenshot of the output #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define LINE_SIZE 1024 void my_error(char *s) { fprintf(stderr, "Error: %s\n", s); perror("errno"); exit(-1); } // This funciton prints lines (in a file) that contain string s. // assume all lines has at most (LINE_SIZE - 2) ASCII characters. // // Functions that may be called in this function: // fopen(), fclose(), fgets(), fputs(),...
C++ Only. Don't use other libraries other than #include <iostream> and using namespace std; Write a function maxTemp which takes a filename as string argument and returns the maximum temperature as float type for the week. Input file will contain temperature readings for each day of the week. Your function should read each line from the given filename, parse and process the data, and return the required information. Your function should return the highest temperature for the whole week. Empty...