python - What changes do I need to make in the following program? -


write program asks user file containing list of items completed , name output file. program should write list, item numbers output file. example, if input file is:

finish python homework. buy milk. laundry. update webpage. 

then output file be:

1.  finish python homework. 2.  buy milk. 3.  laundry. 4.  update webpage. 

i tried following way:

infilenames=input("what file contain list of items?") outfilename=input("what file contain list of items?") infile=open(infilename,"r") outfile=open(outfilename,"w") data=infile.read() countnolines=len(open(infilename).readlines()) p in range(1,countnolines+1):     print(p,data,file=outfile) infile.close() outfile.close() 

but outfile looks like:

1 finish python homework. buy milk. laundry. update webpage. 2 finish python homework. buy milk. laundry. update webpage. 3 finish python homework. buy milk. laundry.  update webpage. 4 finish python homework. buy milk. laundry. update webpags 

*strong text*thanks guys help

old , busted:

infilenames = input("what file contain list of items?") outfilename = input("what file contain list of items?") infile = open(infilenames) outfile = open(outfilename,'w') data = infile.readlines() # list of lines of infile  lineno = 1 line in data:     print(lineno,line.strip(),file=outfile)     lineno += 1  outfile.close() infile.close() 

new hotness:

with open(input_file) in_, open(output_file, "w") out:     i, line in enumerate(in_, 1):         out.write("{}. {}\n".format(i,line.strip())) 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -