[Tutor] Read lines opening with a particular set of characters from a file

Alan Gauld alan.gauld at btinternet.com
Sat Nov 28 07:14:31 EST 2015


On 28/11/15 11:22, Br. Sayan wrote:
> I am doing the following :
> 
> with open('Manwal.txt') as infile, open('Manwal_req.txt','w') as outfile:
>     for line in infile:
>         if line.startswith(('R')):
>             outfile.write(line)
> 
> It is executing without error but returns a blank file. Where is the
> problem?

Are you sure your line actually has multiple lines? The sample you
posted last time showed up on my system as one very long line. I think
it had ^M line breaks in it but my OS didn't recognise them. So if the
file was generated on a different computer/OS to the one you are using
you may need to split the file before processing.

Just a thought.

Try printing the first 5 characters of each line line (line[:5]) and see
if you get what you expect.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list