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...
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 MATLAB function that accepts input vector x and returns the number of local maximums...
Write a MATLAB function that accepts input vector x and returns the number of local maximums of x with value between xmin and xmax. Ask user to input values xmin and xmax at the beginning of the procedure. Use vector x (the vector x from that file consists of 1000 numbers ranging from 0.0044 to 0.67).
Write a parameterized function that takes in a file name as a parameter, reads the file,...
Write a parameterized function that takes in a file name as a parameter, reads the file, calculates the factorial of each number, and displays a formatted output as follows: Factorial of 10 = 3628800 Factorial of 5 = 120
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT