Member Avatar
Rebecca_2

Hi,
I have a quick, and what seems like very stupid question.

I have a list of files in a directory with various extensions. I want to open only the ones with the extension '.txt'.

I have got as far as finding them (and can print their filenames to a new file) but don't seem to be able to open the files themselves to extract their data. Here is the code I have so far:

import os

writer=open('psub', 'w')
writer.close()

for files in os.listdir('.'):
    if files.endswith('.txt'):
        writer=open('psub', 'a')
        print(files, file=writer)
        writer.close()

How then, do I go on to open and read the '.txt' files?

Any pointers would be very appreciated...

Cheers

First of all, you probably don't need to close and open "writer" at every step. It's just creating a lot of overhead. Open it once as you do before the loop and close it once afterward.
As for the reading files, instead of writer=open('psub', 'a'), which I recommend you remove, put reader=open(files) (open for reading is the default). Then change the close to reader.close().

Another approach ...

# find selected files in the working folder using module glob
# glob takes care of upper/lower case
# for instance it finds .txt .Txt .TXT etc.
# glob works with Windows and Unix

import os
import glob

# pick a directory/folder
folder = "C:/temp"
os.chdir(folder)

print("list of all .txt files in folder {} ...".format(folder))
for fname in glob.glob('*.txt'):
    print(fname)

This will create a file of all .txt files in the selected directory ...

import os
import glob

# pick a directory/folder
folder = "C:/temp"
os.chdir(folder)

file_list = glob.glob('*.txt')

with open("my_filelist.txt", "w") as fout:
    fout.write("".join(fname + '\n' for fname in file_list))

This will read the content of all .txt files in a given folder ...

import os
import glob

# pick a directory/folder
folder = "C:/temp"
os.chdir(folder)

print("read content of all .txt files in folder {} ...".format(folder))
for fname in glob.glob('*.txt'):
    print(fname)
    print('-'*70)  # line of 70 dashes, cosmetic
    with open(fname) as fin:
        print(fin.read())
        print('-'*70)