Tips for writing multi-platform Python code

Additional note: When writing multi-platform code in Python, it is best to avoid making assumptions about the environment, if possible. Python helps quite a bit in that regard by providing facilities that performs certain actions regardless of the platform. A very common example is concatenating filenames and folders.

The following code is not cross-platform because it will fail on a Linux machine.
if not folderName[-1:]=='\\': 
folderName+=r'\\'
pathName=folderName+fileName

Python provides a comprehensive library of path handling routines (os.path) that works regardless of the operating system the script runs on: pathName = os.path.join(folderName,fileName)

The same applies to other operations such as splitting a path. For example, consider the following script:
pathParts = pathName.rsplit(‘/’,1) 
if len(pathParts)==2:
folderName,fileName=pathParts

It would work just as well using the following: folderName,fileName = os.path.split(pathName)

These scripts would work on both operating systems, and also cover corner cases you may not have considered (for example, the input is just a folder and not a filename).

Note:

From Forcepoint DLP 10.3 release, the usage of Python 3.6.8 is initiated. This will impose an issue for customers running their own python 2.5 remediation scripts. For suggested solutions, see the Python Remediation Scripts Compatibility Matrix knowledge base article.