I need to write a code that imports a list of files then goes through each file and rewrites only half the file. Cutting off the file at a key word. If I can make my code print the half I need I can make it write new files with the needed information. I am able to read the lines of each file and index the word I am using as the spliting spot but I am unsure where to go from here. I need to figure out how to print everything after the word "END"

here is what i have so far.

import glob
import time
files=glob.glob('mypathname/*.TAB')

for (count, aFileName) in enumerate(files):
    F=open(aFileName, 'r').readlines() 
    for line in F: 
        index= line.find('END ')
        A=line[:index+1]        
        print A

Use

  keep, _ = f.read().split('END',1)

Where would you recammend putting that in my code. I tried to place it at the end but recieved the error 'list' object has no attribute 'read'

Instead of the for loop.

I tried this but still obtained the same error.

for (count, aFileName) in enumerate(files):


    F=open(aFileName, 'r').readlines() 
    keep, _=F.read().split('END ',1)
    print keep 

Take out readlines.

that seems to be working but it seems to be keeping only the lines before 'END' how would i make it keep only the lines after 'END'?

_, keep instead of keep, _

Thank you for your help. You have helped me to be five times more productive at work. And I seem to learn more and more every time i used this website.

Weird syntax. I use c# and it is just a call to index of. That is what I used for [my program that I'm blatantly violating the rules promoting here]. Yes, loop and check each lines return code to see if it found the string your searching. If it didn't find it just do nothing and let the loop continue. Else just get the start position of the string you need and then split on that position. Its easy.

that imports a list of files then goes through each file and rewrites only half the file. Cutting off the file at a key word. If I can make my code print the half I need I can make it write new files with the needed information. I am able to read the

Does anyone know how to use similar code to split my file but while keeping the line your are spliting from. Right now the code pyTony suggested splits my file at 'END' but does not keep the line with 'END' in it. So I need to figure out how to split after 'END' while still keeping that line.

I do not understand as split does not remove the line split value is. If you want to keep the END in text you just concatenate it to end of first or beginning of next part. You might want to check the partition method if you liked to use that.

    for (count, aFileName) in enumerate(files):

        filename = aFileName.split('/')[11]
        day=aFileName.split('/')[10] 
        print day
        print filename
        F=open(aFileName, 'r')#.readlines() 

        #_,keep=F.read().split('END ',1)
        keep,_=F.read().split('END ',1)
        print keep 
        new=open(output+('/')+day+('/')+filename+('test'), 'w')

        for line in keep:
            new.write(line)


        new.close()

using this code for some reason it leaves out the line obtaining "END" and I would like it to have the ability to print everything past that line and everything before that line including that line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.