0

Basically I am trying to convert Line Endings of a large number of files. While I'm doing a lazyread and using regex to replace the line endings

In order to find if the file already exists in desired line ending, I have put an if statement like the one mentioned below:

mreg = b'(?<!\r)\n|\r(?!\n)' if desired_eol == 'CRLF' else b'(?<=\r)\n|\r(?=\n)'
if re.search(mreg, line):
 # Change to desired EOL here
else:
 # (already in desired line ending)
 break

Basically this is working well.

Since I will be reading the file in lazy manner, I would like to know if in a chunk of the whole file the line ending is the desired one, does that mean the entire file is in the desired line ending ?

Kindly please clarify my doubt.

San PJ
  • 11
  • 2
  • Probably, but the safest approach is to read one line at a time up to \n then check whether it ends in \r\n or \n. – Dave S May 18 '19 at 01:41
  • Thank you reply @DaveS But I wanted to know if a file can have multiple line-endings – San PJ May 18 '19 at 02:37
  • I mean like if the file name is `ted.txt` Can we have different line-endings for different lines ? For Example: `This is Line1\r\nThis is Line2\n` – San PJ May 18 '19 at 02:40
  • Not if it was created using only one standard editor, and if nothing was pasted in from another source using the other line ending scheme. In Visual Studio I've had problems pasting Unix \n content into a Windows document with \r\n content because the paste operation did NOT fix the line endings for me. 99% of the time you'll be fine guessing the file is all the same, but that 1% can bite you. For a file that's output from a program, it will match ... unless there is a bug in some part of the code where it only writes \n instead of \r\n for example. – Dave S May 18 '19 at 03:03
  • So it is safe(99% of the time) to assume that the whole file is in desired line-ending @DaveS ? – San PJ May 18 '19 at 03:13

0 Answers0