I need to take a list of files and remove a '.2' from a line in each file. I then need to rewrite the file without the '.2'. Using the code below I am >able to take a single file and do exactly what i need to but when trying to use glob.glob to rewrite a number of files I run into problems and >was wondering if anyone could help.

when i run this code i get the following error.
File "learn.py", line 15, in module

TypeError: coercing to Unicode: need string or buffer, list found

I guess I dont know how to post the code properly can anyone help?

To post a code sample, you can use the Code button on the editing bar just above the editing pane. This should open up a new window where you can paste the code samples. Alternately, you can simply indent the line four spaces, with extra lines between the code and the text.

For more details, go to the link where it says Markdown Syntax: Formatting help, above the editing pane on the right hand side.

Here is the code i have wrote.

   import glob
   import pds_module
   from lxml import etree
   from subprocess import call
    import time
                        files=glob.glob('/mypathname/LS*/*.xml')
                        files.sort()
                        nf=[]   
                        F=open(files, 'r').readlines()  
                        for line in files: 
                            nf+=line.replace('.2','')
                        new=open('test.xml', 'w')
                         for line in nf:
                            new.write(line)
                    new.close()

for line in files:

It does not make sense to call filename line. Your indention is incorrect.

Here is my updated code which i know works for a single file but i am still obtaining the error. TypeError: coercing to Unicode: need string or buffer, list found. when trying to readlines

    import glob
    from lxml import etree
    from subprocess import call

    files=glob.glob('mypathname/LS*/*.xml')
    files.sort()

    F=open(files, 'r').readlines()  
    nf=[]
    for line in F: 
        nf+=line.replace('.2','')
    new=open('test.xml', 'w')

    for line in nf:
        new.write(line)
    new.close()

The problem is that you are trying to use files as a filename, when it is a list of filenames (well, technically file paths, but anyway). You need to either index the specific one of the list elements you want, or else iterate through the list of filenames.

import glob
from lxml import etree
from subprocess import call

files=glob.glob('mypathname/LS*/*.xml')
files.sort()

for (count, aFileName) in enumerate(files):
    F=open(aFileName, 'r').readlines()  
    nf=[]

    for line in F: 
        nf += line.replace('.2','')

    new=open('test{0}.cpp'.format(count), 'w')

    for line in nf:
        new.write(line)

    new.close()

You need to either index the specific one of the list elements you want, or else iterate through the list of filenames.

I understand I need to find a way to go through each file in my list which i called 'files' but i dont understand how to do that. Do you have an example as to what you are talking about?

I was able to take what you sugested and make it work excatly how i need it to but the only problem is a need to run through all the files i have and rewrite each one. I was only able to take your sample code and run the first file.

I am now able to make the prgram you suggested run through all my files and it appears to be writing the last file which would make sense. Do you have any suggestions on how i could change the code to allow the code to write each file by a differenet name?

Now that your got it working, may I give you my version on single file read-write

with open(infile_name) as infile, open(infile_name+'_') as outfile:
        outfile.write(infile.read().replace('.2',''))
os.remove(infile_name)
os.rename(infile_name+'_', infile_name)

Here's a way of how you can do it:

def read():
    l = ["a.txt", "b.txt", "c.txt", "d.txt"]
    outfile = open("e.txt", "a")
    for i in l:
        infile = open(i, "r")
        outfile.write(infile.read().replace('.2', '[Replace sequence]'))

or using pyTony's code:

def read():
    l = ["a.txt", "b.txt", "c.txt", "d.txt"]
    for i in l:
        with open(i, 'r') as infile, open("e.txt", 'a') as outfile:
            outfile.write(infile.read().replace('.2', '[Replace sequence]'))
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.