Example 4

The following example is short but very useful:
# Copy the context xml file into a backup folder 
import sys,os
fileName=sys.argv[1]
# Check the current OS platform 
if sys.platform[:5]=='linux':
tempFolder='/tmp'
elif sys.platform[:3]=='win':
tempFolder=r'c:\temp' # assumes the folder is there!!! else:
# different platform? 
sys.exit(1)
newName=os.path.join(tempFolder,os.path.split(fileName)[1])
# copy! 
newFile=open(newName,'wb') 
oldFile=open(fileName,'rb') 
newFile.write(oldFile.read()) 
newFile.close()

This short piece of code simply copies the provided XML file to a temporary location, usually for debugging or further examination.

Note: Lines 7–13 demonstrate how to write a remediation script that is valid on both Windows and Linux. It uses the “sys.platform” facility to determine the file system structure. The complete string in sys.platform may be linux2, win32, etc., but the script only looks at the first few characters to make a determination.

See Tips for writing multi-platform Python code.