Maneuvering Around Directories w/Python in Windows

Working a lot with SHP/TXT/KML files in Python on Windows, I have found it helpful to refresh my how-to memory :

To find out where you are:

import os
WorkingDirectory = os.getcwd()

To change directories:

import os
os.chdir(“NewWorkingDirectory”)

Really useful is the glob module if you want to read the contents of a directory into a list, and maybe iterate over that list…

import glob
for file in glob.glob(“*.*”):
    print file # print all file names in dir

or

import glob
for shp in glob.glob(“*.shp”):
    print shp # print names of all shapefiles in dir

Obviously, there is much more, e.g. os.path. See here (http://effbot.org/librarybook/os.htm) and here (http://docs.python.org/library/os.path.html).

Leave a Comment

Filed under Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>