I got this question:
Write a function file_size(filename)
that returns a count of the number of characters (example below) in the file whose name is given as a parameter.
It is suggested that you write your function in a file called filesize.py
, and that you save that file in the same directory/folder as any input data files you wish to read before testing your function.
You may assume that when being tested in this CodeRunner question your function will never be called with a non-existent filename.
For example, if data.txt
is a file containing just the following: Hi there! , a call to file_size('data.txt')
should return the value 10. This includes the newline character that will be added to the line when you're creating the file and hit the ENTER key at the end of the line.
I've written this much so far however it says I have a syntax error in line 1, position 0 and position 19?
def file_size(data.txt):
"""counts number of characters in file"""
with open('data.txt', 'r') as file_size:
for line in file_size:
print(len(line))
However, when I remove everything before, it works and shows the correct answer of 10 characters in my data.txt in my folder.
with open('data.txt', 'r') as file_size:
for line in file_size:
print(len(line))
data.txt
is doing?'data.txt'
to the function when you call the function