title: Python: Write all file names in a directory and its subdirectories into a text file
body: <p>My problem is as follows. I want to list all the file names in my directory and its subdirectories and have that output printed in a txt file. Now this is the code I have so far:</p>
  
  <pre><code>import os
  
  for path, subdirs, files in os.walk('\Users\user\Desktop\Test_Py'):
     for filename in files:
       f = os.path.join(path, filename)
       a = open("output.txt", "w")
       a.write(str(f)) 
  </code></pre>
  
  <p>This lists the names of the files in the folders (there are 6) but each new file overwrites the old so there is only one file name in the output.txt file at any given time. How do I change this code so that it writes all of the file names in the output.txt file?</p>