title: Write output of for loop to multiple files with same filename as input file
body: <p>I have a list of filenames in the file filenames.txt. I am opening all the files one by one, and want to write the result of some operation to a different directory but with the same file name. So far, I tried this, but this won't work. Any help?</p>
  
  <pre><code>with open("/home/morty/filenames.txt","r") as f:
      names = [name.strip() for name in f]
  
  for name in names:
      save=open("/home/morty/dir/%name" %name,"w")
      save.write(---some operation---)
  </code></pre>
  
  <p>My question is similar to this, but I don't want the files named by count/enumerate. I want the names to be the same as the input file name:
  <a href="https://stackoverflow.com/questions/24119905/write-output-of-for-loop-to-multiple-files">Write output of for loop to multiple files</a></p>