0
def writeFile(filename):
    prose = r"<?xml version='1.0' encoding='UTF-8'?>"
    startTag = r'<Books>'
    endTag = r'</Books>'
    with open(filename, "+a" ) as f:
        f.write(prose)
        f.write('\n')
        f.write(startTag)
        f.write('\n')
        f.write(endTag)

How do i make this function platform independent so it can work on Windows and Linux/Unix as well Since /n is the new line character on windows.

I am on Python 3.3

jhon.smith
  • 1,963
  • 6
  • 30
  • 56
  • 1
    "Since /n is the new line character on windows" - it isn't. Newline on windows is `\r\n`, two characters. In any case, you're opening the file in text mode (the default), so it should work fine. – user2357112 Mar 14 '14 at 04:39
  • 1
    python handles newline conversion for you. `\n` just works. – roippi Mar 14 '14 at 04:40
  • You may want to specify an encoding of `'utf-8'` to the `open` call, though. – user2357112 Mar 14 '14 at 04:41

1 Answers1

6

You need to look in to os module. Especially check os.linesep and os.sep.

os.linesep will give you give correct new line separator, you don't need to check the platorm/os version

os.sep will give you separator for pathname components and again you don't need to check the platform/os version

MA1
  • 2,767
  • 5
  • 35
  • 51