0

I am trying to create a function for a task I mentioned earlier for adding a new field to some files that are stored in one folder. I read about the function method in Python and followed but it does not work. I would be happy if someone could guide me about it.

def newfield(infile,outfile):
    infile.readlines()
    outfile = ["%s\t%s" %(item.strip(),2) for item in infile]
    outfile.write("\n".join(output))
    outfile.close()
    return outfile

and then I try to call it:

a = open("E:/SAGA/data/2006last/test/325145404.all","r")
b = open("E:/SAGA/data/2006last/test/325145404_edit.all","w")
newfield(a,b)

Moreover, when we create a function, should we call it in a same python shell or we can same it as a file and then call it?

2
  • What doesn't work? What output did you expect to get, and what output did you find instead? What does your input look like? Commented Apr 3, 2013 at 11:36
  • it gives this error:Traceback (most recent call last): File "<pyshell#21>", line 1, in <module> newfield(a,b); File "<pyshell#19>", line 4, in newfield outfile.write("\n".join(output)) AttributeError: 'list' object has no attribute 'write' Commented Apr 3, 2013 at 11:44

2 Answers 2

1

Looks like the problem is you're assigning the list you build to the same name as the output file. You're also reading all of the lines out of the file and not assigning the result anywhere, so when your list comprehension iterates over it, it's already at the end of the file. So you can drop the readlines line.

def newfield(infile,outfile):
    output = ["%s\t%s" %(item.strip(),2) for item in infile]
    outfile.write("\n".join(output))
    outfile.close()
    return outfile
Sign up to request clarification or add additional context in comments.

12 Comments

I tried ur suggestion. when I call this function as : a = open("E:/SAGA/data/2006last/test/325155405.all","r") b = open("E:/SAGA/data/2006last/test/325155405_edit.all","w") newfield(a,b); it does nt create a new outfile
Is there an error? Also did you start a new python prompt after updating the code?
yes the error is : Traceback (most recent call last): File "<pyshell#50>", line 1, in <module> newfield(a,b); File "<pyshell#46>", line 3, in newfield outfile.write("\n".join(output)) AttributeError: 'list' object has no attribute 'write'
maybe my problem is the way to call it. I wrote the function in python GUI and called afterwards in the same page,is it correct?
That error looks to be from the orignal code you posted, rather than the code above
|
0

The line infile.readlines() consumes the whole input file. All lines are read and forgotten because they are not assigned to a variable.

The construct ["%s\t%s" %(item.strip(),2) for item in infile] is a list comprehension. This expression returns a list. By assigning it to the variable outfile the old value of outfile - probably a file object - is forgotten and replaced by the list returned by the list comprehension. This list object does not have a write method. By assigning the list to another variable the file object in outfile is preserved.

Try this:

def newfield(infile,outfile):
    items = ["%s\t%s" %(item.strip(),2) for item in infile]
    outfile.write("\n".join(items))
    outfile.close()
    return outfile

6 Comments

I tried ur suggestion. when I call this function as : a = open("E:/SAGA/data/2006last/test/325155405.all","r") b = open("E:/SAGA/data/2006last/test/325155405_edit.all","w") newfield(a,b); it gives this error :<closed file 'E:/SAGA/data/2006last/test/325145404_ed.all', mode 'w' at 0x02CE8650>
sorry would u plz explain more what do u exactly mean?I do not get u.
thank you for ur help. I tried the new code u recommended. when I call ,I receive this error: <closed file 'E:/SAGA/data/2006last/test/325145404_ed_2.all', mode 'w' at 0x02CE86A8> what do u think it refers to?
Do you use the returned file after calling the function?
what does this error means? File "<pyshell#81>", line 3, in newfield1 outfile.write("\n".join(items)) ValueError: I/O operation on closed file
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.