-1

How to read and write all the file with *.txt extension in a loop. I would like to write output to same file name.

My code:

PWD1 = os.getcwd()
f = open(PWD1+'/' +DirPath + ".txt", "r")
g = open(PWD1+'/' +DirPath + ".txt", "w")
for line in f:
    if line.strip():
        g.write("\t".join(line.split()[2:]) + "\n") # removes first two columns from text file.
A.close()
f.close()
g.close()

text1.txt

2 33 /home/workspace/aba/abga
22 4 /home/home

text2.txt

1 44 /home/workspace/aba/abga
24 5 /home/home

desired output

text1.txt

/home/workspace/aba/abga
/home/home

text2.txt

/home/workspace/aba/abga
/home/home

Please note that there are several .txt files in this directory. I would like to loop through all of them.

Mihir
  • 557
  • 1
  • 6
  • 28
  • You're not reading and writing from all the text files, you're reading from one and writing to one – Zoe Aug 21 '18 at 18:43
  • 1
    It's generally not a good idea to obliterate the file that you're trying to read from. ;) If these files are small, you can read the whole thing into RAM, modify the data, and then write it back to the file. – PM 2Ring Aug 21 '18 at 18:44
  • true, but then directory will have lots of unwanted files. – Mihir Aug 21 '18 at 18:46
  • There's a good summary of the different file modes [here](https://stackoverflow.com/a/23566951/4014959). You can use one of those fancy file modes. Or just open the file in 'r' mode, read the data, close the file. Modify the data then re-open the file in 'w' mode to write the new version. You're less likely to mess stuff up if you make a mistake that way (or if the power dies while your program's running). – PM 2Ring Aug 21 '18 at 18:47
  • Alternatively, do it line by line like gahooa suggests. That's a great strategy if your files are too large to load into RAM. – PM 2Ring Aug 21 '18 at 18:53

1 Answers1

1

In this example we get a list of all .txt files in the current directory, process each one into a new file (same name + .2), and then delete the original and replace it with the .2 file.

import glob
files = glob.glob('*.txt')
for f in files:
   with open(f, 'rt') as reader:
       with open(f+'.2', 'wt') as writer:
          for line in reader:
              writer.write("\t".join(line.split()[2:]) + "\n") #your logic not mine
   os.remove(f)
   os.rename(f+'.2', f)
gahooa
  • 131,293
  • 12
  • 98
  • 101
  • 1
    FWIW, in recent Python versions you can open multiple files in parallel in one `with` statement. It's not a big deal, but it does save a level of indentation. – PM 2Ring Aug 21 '18 at 18:55