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

Laura Creighton lac at openend.se
Sat Nov 28 06:43:25 EST 2015


In a message of Sat, 28 Nov 2015 16:52:07 +0530, "Br. Sayan" writes:
>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?

Just try to print the lines, instead of writing them to the file.
If nothing gets printed then your infile name is wrong, or you aren't
running in the directory where that file is, or, the lines don't
really start with R after all.  If things print, then you aren't
creating a new outfile properly, possibly a permissions issue.

Laura




More information about the Tutor mailing list