Write a Python function that takes a filename as a parameter and returns the string 'rows' if a file was written row by row, and 'columns' if the file was written column by column. You would call the function with a command like filetype = myfunc(filename).
Code explaination :
We have created function that takes a filename as parameter and returns row if file is written row by rows else returns columns
#function defined
def myfunc(filename):
openFile = open("example.txt", "r")
fileData = openFile.read()
rowData = fileData.split("\n")
rowCount = 0
for a in rowData:
if a:
rowCount += 1
# if row count is more than 1 then it returns rows
if rowCount > 1:
return "rows"
#else it will return column
else:
return "columns"
Get Answers For Free
Most questions answered within 1 hours.