Python: Basic file path management

Code example of how to get the current directory, change directories, and set the output to someplace specific – designed to work on both windows and unix.

# Finds the directory that a program was run from, change the directory 
# and creates an output file within a subdirectory.  This code is designed 
# to run on both mac and windows.

import os

# what is the current working directory? notice the difference when running 
# from unix/mac verses windows
os.getcwd()


# change the current working directory.  use forward slash regardless of unix 
# or windows
os.chdir("D:/capplan")
wd=os.getcwd()
print (wd)

# create a file in a subdirectory from the current working directory
# this method makes the results operating system agnostic
# each entry in the parameters is the next directory level, ending with the 
# file name os.path.join inserts the appropriate separators

file_path = os.path.join(wd,"xml","output.xml")
print (file_path)
f=open(file_path,'w')

# file is now open as 'f'