title: Python - Counting the number of characters in files
body: <p>my question is that I am trying to write a function that counts the number of characters in some specified .txt file. I have come up with the following code;</p>
  
  <pre><code>def file_size(filename):
    with open('data.txt', 'r') as file:
      filename = file.read()
      len_chars = sum(len(word) for word in filename)
      return len_chars
  </code></pre>
  
  <p>This function works when I call it with <code>print(file_size('data.txt'))</code> and gives me the correct output. However, I am wondering how I can change my code so that it can count the number of characters in ANY .txt file saved in the same directory as the program itself rather than just data.txt when I call it with <code>print(file_size('filename'))</code>. For example, if there are 10 .txt files saved into the same directory as my program, I would like to be able to run this program for any of those files without having to  input the name of the file in my function but rather use the name of the file when I am calling the function. Thanks in advance for any help.</p>