Skip to content

Document that Create File and Append To File convert \n to \r\n on Windows starting from RF 3.1 #3097

Closed

Description

@mytzenka

Since upgrading to version 3.1.1 of robot framework (from version 3.0.4) I encounter the following.

When I create a file and then write lines to that file - which I end with ${\n} because I want each on a new line - an extra new line character (CR actually) gets added. This did not happen before and this is unwanted behaviour.

Example test to reproduce
`
*** Settings ***
Library OperatingSystem

*** Test Cases ***
Test Me
create file C:/Users/Testfile.txt
append to file C:/Users/Testfile.txt Line1${\n}
append to file C:/Users/Testfile.txt Line2${\n}
append to file C:/Users/Testfile.txt Line3${\n}
`

Output
image

Expected output
image

System details
OS: Windows 10 Enterprise 1703
Robot Framework version: 3.1.1
Python version: 2.7.12

Activity

  1. mytzenka commented on Feb 20, 2019

    @mytzenka
    Author

    Perhaps the problem is in the handling of built-in variable ${\n}?
    What I see upon further testing is that if I use '\n' instead of '${\n}' in my 'append to file', my result is like the expected output again, both on the old and new version of Robot Framework.

  2. bartkl commented on Feb 26, 2019

    @bartkl

    Here's a potentially relevant difference in OperatingSystem.py between versions:

    # 3.1.1
        def _write_to_file(self, path, content, encoding=None, mode='w'):
            path = self._absnorm(path)
            parent = os.path.dirname(path)
            if not os.path.exists(parent):
                os.makedirs(parent)
            # io.open() only accepts Unicode, not byte-strings, in text mode.
            # We expect possible byte-strings to be all ASCII.
            if PY2 and isinstance(content, str) and 'b' not in mode:
                content = unicode(content)
            with io.open(path, mode, encoding=encoding) as f:
                f.write(content)
            return path
    #3.0.4
        def _write_to_file(self, path, content, encoding=None, mode='w'):
            path = self._absnorm(path)
            parent = os.path.dirname(path)
            if not os.path.exists(parent):
                os.makedirs(parent)
            if encoding:
                content = content.encode(encoding)
            with open(path, mode+'b') as f:
                f.write(content)
            return path
  3. pekkaklarck commented on Feb 26, 2019

    @pekkaklarck
    Member

    First of all, ${\n} is either CRLF or LF, depending on the OS, when \n is always just LF. It seems that earlier files were opened in binary mode in this particular case and that meant that all characters/bytes were written to files as-is. Nowadays files are opened in text mode, which means that on Windows LF is automatically converted to CRLF (and CRLF is turned into CRCRLF). That explains why using ${\n} on Windows causes problems with RF 3.1 but I don't understand how \n works with earlier versions. Could you @MichielYKamp test this again?

    This change wasn't actually planned, but I still consider the current behavior better than the old one. I'd thus prefer keeping the current behavior but the change should still be documented somewhere.

  4. mytzenka commented on Feb 27, 2019

    @mytzenka
    Author

    Thank you for your reply @pekkaklarck .

    I understand your explanation. I can see how opening a file in text mode by default may be better then opening it in binary mode, but the fact that CRLF is than converted into CRCRLF on Windows I still find strange. If I am on Windows and already giving CRLF in the append to file, I think it shouldn't convert my CRLF again because it is not needed. It should only convert LF to CRLF.

    I tested \n on the old version of RF and, as expected, I get just LF in my lines there.
    image

    If nothing will be changed then I agree that making this behaviour clear in the documentation, for both ${\n} and append to file (and similar keywords) would be good.

  5. bartkl commented on Feb 27, 2019

    @bartkl

    I don't think I quite understand. From the documentation of the io.open method:

    On input, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller

    In the 3.1.1 code snippet I provided above, io.open is called such that newline is None, so by the quoted explanation I would expect the ${\n} (as well as any other line ending characters due to the universal newlines being enabled) to ultimately be converted to \n (or LF).

    I would appreciate it if someone could clear this up for me, thanks.

  6. pekkaklarck commented on Feb 27, 2019

    @pekkaklarck
    Member

    @bartkl "On input" isn't very clear, but it seems to mean reading in this case. What it means in practice is that if you have a file with content first\r\nsecond\r\n, you'll by default get first\nsecond\n when you read it with Python. When writing, Python will automatically convert \n to \r\n. Newline handling is also affected by mode argument. In text mode (default) conversion happens but in binary mode it doesn't:

    >>> p = 'test.txt'
    >>> open(p, 'w').write('first\nsecond\n')
    >>> open(p, 'rb').read()
    'first\r\nsecond\r\n'
    >>> open(p, 'r').read()
    'first\nsecond\n'
  7. pekkaklarck commented on Feb 27, 2019

    @pekkaklarck
    Member

    @MichielYKamp Can you confirm the following?

    • With RF 3.1.x \n is converted to \r\n and ${\n} is converted to \r\r\n.
    • With RF 3.0.x (and earlier) \n results to \n and ${\n} yields \r\n.

    That would be logical based on the code and seems to be what your results are as well. Such a change definitely wouldn't be good without an explicit note in the release notes.

    Somewhat strange nobody else has reported this issue. I would assume most people have used \n and tools reading files generally handle the resulting \n just fine. With newer RF versions \n yields \r\n which tools handle even better.

    Something needs to be done for this in RF 3.1.2. Alternatives include.

    • Switch to the old behavior. I don't like this too much because the current one is, in my opinion, better and also consistent with how Python handles files.
    • Just document the current behavior and mention also that there have been changes in RF 3.1.
    • Add a new argument to handle newline conversion similarly as Python has. Would allow creating files with \n as a separator also on Windows. Documentation enhancements would still be needed.

    Regardless what is done, should also add a note to RF 3.1 release notes about this change.

  8. added this to the v3.1.2 milestone on Feb 27, 2019
  9. bartkl commented on Feb 27, 2019

    @bartkl

    Oh, of course, it's opened for writing. Then the documentation indeed does state exactly what's happening:

    On output, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep.

    Also, to extend on your example with a case that reflects the original problem:

    >>> f = 'test.txt'
    >>> open(f, 'w').write('first\r\nsecond\n')
    >>> open(f, 'rb').read()
    'first\r\r\nsecond\r\n'
  10. mytzenka commented on Feb 27, 2019

    @mytzenka
    Author

    @MichielYKamp Can you confirm the following?

    • With RF 3.1.x \n is converted to \r\n and ${\n} is converted to \r\r\n.
    • With RF 3.0.x (and earlier) \n results to \n and ${\n} yields \r\n.

    Yes, I can confirm this. Thank you.

  11. pekkaklarck commented on Apr 16, 2019

    @pekkaklarck
    Member

    Thanks for confirmation @MichielYKamp.

    Been thinking this a bit more and here are decisions:

    1. Keep the behavior as-is. It's more convenient in general and being consistent with Python is a plus too. Based on there not being more issues about this, the change doesn't seem to affect that many users.

    2. Document the change. This includes mentioning it in RF 3.1 release notes and updating keyword documentation.

    3. Add explicit tests.

    4. Don't add new argument to control newline behavior. Because old versions wouldn't have this argument, it wouldn't help handling the backwards-incompatible change anyway. If such an argument is considered useful otherwise, a separate issue can be submitted.

  12. changed the title [-]Extra new line inserted with 'OperatingSystem.append to file'[/-] [+]Document that `Create File` and `Append To File` convert `\n` to `\r\n` on Windows starting from RF 3.1[/+] on Apr 16, 2019
  13. pekkaklarck commented on Apr 16, 2019

    @pekkaklarck
    Member

    Forgot to mention that with Create File a good workaround is using Create Binary File that doesn't do newline conversion with any version. If content contains non-ASCII characters, Encode String To Bytes needs to be used first to handle them, though.

  14. added theissue type on Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions