<< Main Manual

Tutorial #0.7. Filesystem

TIP #1: The following examples are a crash course in Python programming showing the features that are most beneficial with the minimal amount of extraneous information. You can type the example code directly within PhysImage's 'Jython Console' or load the example files (*.py links below) directly from PhysImage as a demonstration, or test the code out with an independent installation of Python 2.7 on your preferred OS platform. These examples are independent of any specific PhysImage features and meant to be a succinct demonstration of very useful general Pythonic features.

TIP #2: The ">>> ", in the below example, is meant to represent PhysImage's 'Jython Console' prompt, while '#' in a line represents a comment (used to annotate Python code) and does not do anything. Therefore,
the text following the '#' is only for your benefit to understand the next bits of code and is not required to be typed in.

For a more expansive tutorial of the general Python programming language, see the Official Python Tutorial.

Example

The following code can be loaded within PhysImage from this file or typed in manually.

>>> Useful functions for interacting with the file system
>>>
>>> # copying a file
>>> import shutil
>>> shutil.copy('output.txt', 'output2.txt')
>>>
>>> # renaming a file
>>> import os
>>> os.rename('output2.txt', 'output3.txt')
>>>
>>> # getting a list of files in the current directory
>>> files = os.listdir('.')
>>> print "Files in the current directory: " + str(files)
>>>
>>> # file paths on Windows:
>>> ### absolute path
>>> filepath = "C:\\Python25\\blah\\file.txt"
>>> print "absolute path (Windows): " + filepath
>>> ### relative path
>>> filepath = "directory\\subdirectory\\file.txt"
>>> print "relative path (Windows): " + filepath
>>>
>>> # file paths on Unix/Linux/OS X
>>> ### absolute path
>>> filepath = "/home/john/blah/file.txt"
>>> print "absolute path (Linux/OS X): " + filepath
>>>
>>> ### relative path
>>> filepath = "directory/subdirectory/file.txt"
>>> print "relative path (Linux/OS X): " + filepath
>>>
>>> # creating a file path in a platform-independent way
>>> ### relative path
>>> filepath = "directory" + os.sep + "subdirectory" + os.sep + "file.txt"
>>> print "relative path(platform-independent) #1: " + filepath
>>>
>>> # is equivalent to
>>> directoryPath = ["directory", "subdirectory", "file.txt"]
>>> filepath = os.sep.join(directoryPath)
>>> print "relative path (platform-independent) #2: " + filepath
>>>
>>> # getting the absolute path of the current directory
>>> currentDirectory = os.path.abspath(".")
>>> print "currentDirectory #1: " + currentDirectory
>>>
>>> # is equivalent to
>>> currentDirectory = os.path.abspath(os.path.curdir)
>>> print "currentDirectory #2: " + currentDirectory
>>> # is equivalent to
>>> currentDirectory = os.getcwd()
>>> print "currentDirectory #3: " + currentDirectory
>>>
>>> # determine if a file/directory exists
>>> if os.path.exists('output.txt'):
...    print "output.txt exists!"
... else:
...    print "output.txt doesn't exist"
...
>>> # create a directory if it doesn't already exist
>>> newDirectory = 'temp'
>>> if not os.path.exists(newDirectory):
...    print "Making a new directory called: " + newDirectory
...    os.mkdir(newDirectory)
...
>>> # see the following links for lots of other useful file system related functions:
>>> http://docs.python.org/library/os.html
>>> http://docs.python.org/library/os.path.html
>>> http://docs.python.org/library/shutil.html
>>>


After the data is in the WaveManager it got manipulated or plotted as a chart in the previously demonstrated ways.

<< Tutorial #0.6. Files

<< Main Manual