Question

In: Computer Science

On Python Write a function that accepts a relative file path as parameter and returns number...

On Python

Write a function that accepts a relative file path as parameter and returns number of non-empty lines in the file. Test your function with the provided sample file studentdata.txt.

Solutions

Expert Solution

This question can be solved in two ways:

Way 1. If a line with spaces is considered empty

Way 2. If a line with spaces is not considered empty

I'll solve this question in both ways:

Way 1:

def countNonEmptyLines(path): #Required Function

    fl = open(path,"r")

    lines = fl.readlines()

    count = 0

    for line in lines:

        #Checking if the line is empty or not

        #strip() returns true on encountering spaces

        if(line and line.strip()):

            count = count + 1

    return count

nonEmptyLines = countNonEmptyLines("./studentdata.txt")

#The lines with just spaces are also considered as empty

print("The number of non empty lines in the given file are: " + str(nonEmptyLines))

Way 2:

def countNonEmptyLines(path): #Required Function

    fl = open(path,"r")

    lines = fl.readlines()

    count = 0

    for line in lines:

        #Checking if the line is empty or not

        if(line):

            count = count + 1

    return count

nonEmptyLines = countNonEmptyLines("./studentdata.txt")

#The lines with just spaces are not considered as empty

print("The number of non empty lines in the given file are: " + str(nonEmptyLines))


Related Solutions

Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns...
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns the sum of all the odd numbers that are less than or equal to the input number. The function should return 0 if the number is not positive. For example, if 15 is passed as argument to the function, the function should return the result of 1+3+5+7+9+11+13+15. If -10 is passes, it shall return 0. You shall run the program test the result at...
Create your own function in Java that accepts one input parameter and returns a float number....
Create your own function in Java that accepts one input parameter and returns a float number. You decide the theme.
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Can someone do this python program? Write a function that takes a number as a parameter...
Can someone do this python program? Write a function that takes a number as a parameter and then prints out all of the factors for that number.
Write a method sumTo that accepts an integer parameter n and returns the sum of the...
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words: sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should print an error message and return -1 if passed a value less than 0. Include a loop. Please help for Java programming.
Write a function file that accepts each of the four vectors as inputs. The function file...
Write a function file that accepts each of the four vectors as inputs. The function file should plot the data on different graphs in the same window. You need to add axis labels and graph titles. time- 0 5 10 15 20 25 30 A1- 0 7 11 19 15 14 7 A2- 0 10 15 21 16 11 13 A3- 0 9 13 17 22 25 21
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT