Go to the table of contents Go to the previous page You are at the end of the document View or print as PDF
Creating Remediation Scripts for Forcepoint DLP > Tips for writing multi-platform Python code
Tips for writing multi-platform Python code
Creating Remediation Scripts | Forcepoint DLP | v8.4.x, v8.5.x, v8.6.x
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).
©2018 Forcepoint. Forcepoint and the FORCEPOINT logo are trademarks of Forcepoint. Raytheon is a registered trademark of Raytheon Company. All other trademarks used in this document are the property of their respective owners.

Go to the table of contents Go to the previous page You are at the end of the document View or print as PDF
Creating Remediation Scripts for Forcepoint DLP > Tips for writing multi-platform Python code
Copyright 2018 Forcepoint. All rights reserved.