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.

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).

Advertisements

Leave a comment

Filed under Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s