[Tutor] printing files
Friends, My files are like below file1 file2 RemarkRemark --- --- I have huge number of such files. I want to concatenate all files in one huge file. I could do it with a script. But i want to omit the first line (ie Remark in each file) and concatenate. How to do the same ? flist=glob.glob(*.txt) out=open('all','w') for files in flist: handle=open(flist).readlines() printout, handle -- Here i want to write only from second line. I dnt want to loop over handle here and putting all lines except the first one in another variable. Is there any fancy way of doing it. out.close() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing files
On Thu, Mar 26, 2009 at 10:42 AM, Bala subramanian bala.biophys...@gmail.com wrote: printout, handle -- Here i want to write only from second line. I dnt want to loop over handle here and putting all lines except the first one in another variable. Is there any fancy way of doing it. Without changing anything else, you could do it with a slice: flist=glob.glob(*.txt) out=open('all','w') for files in flist: handle=open(flist).readlines() printout, handle[1:] # start with second item (indexes start at 0, remember) and go to end out.close() -- www.fsrtechnologies.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing files
On Thu, Mar 26, 2009 at 10:56 AM, Marc Tompkins marc.tompk...@gmail.comwrote: Without changing anything else, you could do it with a slice: You should probably also close your input files when you're done with them. -- www.fsrtechnologies.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing files
Bala subramanian bala.biophys...@gmail.com wrote for files in flist: handle=open(flist).readlines() printout, handle printout, handle[1:] Should do it? You might need to handle line endings though... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing files
Use '\n'.join(handle[1:]) It will create a string from your list with newline as separator. Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ From: Bala subramanian bala.biophys...@gmail.com To: Alan Gauld alan.ga...@btinternet.com Sent: Thursday, 26 March, 2009 6:11:59 PM Subject: Re: [Tutor] printing files yes you are right, When i use the following printout, handle[1:] In the out file, it saves the lines as a list rather than as a string. How to avoid this. Bala On Thu, Mar 26, 2009 at 7:05 PM, Alan Gauld alan.ga...@btinternet.com wrote: Bala subramanian bala.biophys...@gmail.com wrote for files in flist: handle=open(flist).readlines() printout, handle printout, handle[1:] Should do it? You might need to handle line endings though... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing files
On Thu, Mar 26, 2009 at 2:58 PM, ALAN GAULD alan.ga...@btinternet.com wrote: Use '\n'.join(handle[1:]) It will create a string from your list with newline as separator. The lines from readlines() include the newlines already. When i use the following printout, handle[1:] In the out file, it saves the lines as a list rather than as a string. How to avoid this. use out.writelines(handle[1:]) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing files
Kent Johnson ken...@tds.net wrote On Thu, Mar 26, 2009 at 2:58 PM, ALAN GAULD alan.ga...@btinternet.com wrote: Use '\n'.join(handle[1:]) It will create a string from your list with newline as separator. The lines from readlines() include the newlines already. Ah, OK, I couldn't remember if readlines stripped them off or not. printout, handle[1:] In the out file, it saves the lines as a list rather than as a string. use out.writelines(handle[1:]) Or if you really want to use the print style printout, ''.join(handle[1:]) ie join the lines using an empty string. Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor