0

I have a series of files similar to that
1A2B_A.txt, 1A2B_2C.txt, 1A2B_DF.txt,
1B23_D.txt, 1B23_B.txt, 1B23_25.txt, and many more
I want to combine each three files starting with the same four code before the "_"

Thanks in advance

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by pyTony
0

Something like this, can not test without those files:

import os
## assumes curdir is directory with the text files to process
if not os.path.isdir(os.path.join(os.curdir,'joined')):
    os.mkdir(os.path.join(os.curdir,'joined'))
    
for textfile in (fn for fn in os.listdir(os.curdir)
                 if fn.endswith('.txt') and fn[4]=='_'):
    
    begin,_,end = textfile.partition('_')
    
    open( os.path.join(os.curdir,
                       'joined',
                       begin+'.txt'),
          'a').write(open(textfile).read())

Edited by pyTony: fix begin+'.txt'

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.