0

So, I have created some code which goes a little bit like this:

import os
while True:
    DIRFIND = input(">")
    DIRREAD = os.system("dir %s > tmp" % (DIRFIND))
    with open("tmp","r") as myfile:
        DIRREAD = myfile.read().replace("\n", "")
    print(DIRREAD)

However nothing is written in the tmp file that is created and I have a sneaking suspicion that this has something to do with the syntax of the %s / % part of the code resulting in an error. Any ideas??

  • You're opening a file for reading "r", reading lines from it, and then modifying those lines in your local code (not in the file itself). – Andrew_CS Oct 21 '16 at 14:53
  • Files are not modified magically by calling replace, you need to write the changes to the file. – Padraic Cunningham Oct 21 '16 at 14:55
  • Possible duplicate of [Read in file - change contents - write out to same file](http://stackoverflow.com/questions/7194665/read-in-file-change-contents-write-out-to-same-file) – Padraic Cunningham Oct 21 '16 at 14:56
  • What is the best way you would recommend fixing this? How would I modify the file itself if the only file created is the output from the executed command in the terminal? – Reuben Allison Oct 21 '16 at 14:56
  • It is all in the linked dupe. – Padraic Cunningham Oct 21 '16 at 14:59
  • OK, so that tells me how to write in a file - what I'm asking is how to write something in the os.system("dir %s > tmp" % (DIRFIND)) where DIRFIND is something like C:\Users\, and therefor the command output to run from dir C:\Users\ ... – Reuben Allison Oct 21 '16 at 15:05

0 Answers0