10 Replies - 510 Views - Last Post: 08 November 2012 - 08:17 PM Rate Topic: -----

#1 medaa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Writing into output file problem.

Posted 08 November 2012 - 06:59 PM

Im having trouble writing my stuff back into the output file. I keep getting "builtins.TypeError: must be str, not list" for the line "ofile.write(lines)", can someone please explain why this is happing, how do I fix it, thanks.

[import os.path
while True:
try:
name1=input("Enter input name:")
infile=open(name1,"r")
name2=input("Enter output name:")
while(os.path.isfile(name2)):
name2=input("File exists. Enter new output name:")
ofile=open(name2, "w")
break
except:
print("Error in code")



lines=[]
for line in infile:
lines.append(line)
for x in range(len(lines)):
print(lines.pop())
ofile.write(lines)

infile.close()
ofile.close()]

Is This A Good Question/Topic? 0
  • +

Replies To: Writing into output file problem.

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Writing into output file problem.

Posted 08 November 2012 - 07:06 PM

You're writing the whole lines list to the file, which is a list file type. Try doing lines[x] instead, since you iterate through the list anyway.
Was This Post Helpful? 0
  • +
  • -

#3 medaa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Re: Writing into output file problem.

Posted 08 November 2012 - 07:14 PM

lines=[]
for line in infile:
lines.append(line)
for x in range(len(lines)):
print(lines.pop())
ofile.write(lines[x]) <-- if i do this i get the error list index out of range.

infile.close()
ofile.close()]
Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Writing into output file problem.

Posted 08 November 2012 - 07:26 PM

Oh, it's because you pop em before writing. Why do you do that? Try removing that, then run the thing.

If the purpose of using pop is just for printing, don't use it. Just use lines[x]. If it has some use, then tell us how your program should run. ^^
Was This Post Helpful? 0
  • +
  • -

#5 medaa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Re: Writing into output file problem.

Posted 08 November 2012 - 07:32 PM

the prgram needs to take a file and reverse the content(last line first) within, then write the reversed content in a new file
Was This Post Helpful? 0
  • +
  • -

#6 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Writing into output file problem.

Posted 08 November 2012 - 07:41 PM

If that's the case, you should declare another variable that will hold the return value from the pop and then write that one, like so:

>>> i = 0
>>> l = [1,2,3,4,5]
>>> for x in range(len(l)):
...     i = l.pop()
...     print (i)
...
5
4
3
2
1



Does that do it?
Was This Post Helpful? 0
  • +
  • -

#7 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Writing into output file problem.

Posted 08 November 2012 - 07:52 PM

Also, you could directly write the pop without resorting to another variable, just like you originally did but the problem was you printed it using pop so the value's gone. What you should do is this instead:
oFile.write(l.pop())


Was This Post Helpful? 0
  • +
  • -

#8 medaa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Re: Writing into output file problem.

Posted 08 November 2012 - 08:01 PM

thank you a lot, both methods work nicely.
Was This Post Helpful? 0
  • +
  • -

#9 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Writing into output file problem.

Posted 08 November 2012 - 08:03 PM

No problem, glad to know I helped. ^^
Was This Post Helpful? 0
  • +
  • -

#10 medaa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 24-October 12

Re: Writing into output file problem.

Posted 08 November 2012 - 08:06 PM

can i ask one more question, if i had this code

[import os.path
while True:
try:
name1=input("Enter input name:")
infile=open(name1,"r")
name2=input("Enter output name:")
while(os.path.isfile(name2)):
name2=input("File exists. Enter new output name:")
ofile=open(name2, "w")
break
except:
print("Error in code")

dan=0
count=0
ls=0
srh=0
bd=0
for line in infile:
if line!='' or line[0]!='#':
lst=line.split(" ")
count=count+1

while "" in lst:
lst.remove("")

if len(lst)==4:

[color,size,flesh,clss]=lst
if clss=="dangerous":
dan=dan+1
if size=="large" and clss=="safe":
ls=ls+1
if color=="brown" and clss=="dangerous":
bd=bd+1
if clss=="safe" and color=="red" or flesh=="hard":
srh=srh+1



print("Total number of animals is",count)
print("Total number of dangerous animals",dan)
print("Number of large animals that are safe",ls)
print("Percentage of animals that are brown and dangerous",bd,"%")
print("Number of safe animals with red color or hard flesh",srh)]

how do you write the print statements in the new file??
Was This Post Helpful? 0
  • +
  • -

#11 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Writing into output file problem.

Posted 08 November 2012 - 08:17 PM

Hmm, shouldn't it be as simple as file.write("write what you want")?
>>> import os.path
>>> infile = open('newFile.txt','w')
>>> import os.path
>>> outfile = open('newFile.txt','w')
>>> outfile.write("Here's a line for all your troubles.")
>>> outfile.close()

#File content
Here's a line for all your troubles.



You can also save them first in the string so you can do formatting and stuff before writing it:
>>> boy, girl = "John","Jane"
>>> s = "Congratulations to the newly weds {0} and {1}!".format(boy,girl)
>>> outfile = open('newFile.txt','w')
>>> outfile.write(s)
>>> outfile.close()

#File content
Congratulations to the newly weds John and Jane!



How would you want for it to appear in the file?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1