i,m reading some lines from a file and i'm checking whether each line has windows type of CRLF or not. If either '\n' or '\r' is absent in any line, it has to report an error. I tried with the below code, even if the line doesnt have '\r', it is not reporting any error
Open_file = open(File_Name,'r').readlines()
while Loop_Counter!= Last_Line:
Line_Read = Open_file[Loop_Counter]
if('\r\n' in Line_Read):
pass
else:
print Loop_Counter
Thank you
This isn't working because Loop_Counter is never adjusted at all; whatever the initial value is, it's not changing and the while loop either runs indefinitely or never passes. Your code is pretty unclear here; I'm not sure why you'd structure it that way.
What you're suggesting would be easier to do like this:
infile = open(filename, 'rb')
for index, line in enumerate(infile.readlines()):
if line[-2:] != '\r\n':
print index
The 'rb' argument is necessary to make sure the newlines are read as \r\n and not just as \n.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With