jhon.smith
jhon.smith

Reputation: 2043

platform independent way of writing files

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

Upvotes: 0

Views: 547

Answers (1)

MA1
MA1

Reputation: 2847

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

Upvotes: 6

Related Questions