3

What is os.sep and when should i use os.sep while scripting for arcgis ?

For example in below code i don't understand why should i use os.sep .

import arcpy
from arcpy import env
import os

# Set the workspace for the ListFeatureClass function
#
env.workspace = "c:/base"

# Use the ListFeatureClasses function to return a list of 
#  all shapefiles.
#
fcList = arcpy.ListFeatureClasses()

# Copy shapefiles to a file geodatabase
#
for fc in fcList:
  arcpy.Copy_management(fc, "d:/base/output.gdb" + os.sep + fc.rstrip(".shp"))
1
  • 1
    Please note that this question is one of those "borderline" ones that is of (at best) marginal interest on a GIS site, because (a) it clearly has nothing special to do with the GIS program you're working with and (b) an obvious search on the Web immediately turns up an answer (Google "os.sep" for instance). We're letting it stand because it has been answered and you're a relatively new user, but future questions meeting these criteria will be closed or migrated to a purely programming site.
    – whuber
    May 19, 2011 at 15:23

2 Answers 2

10

Just use os.path.join(), noting the caveat for Windows at http://docs.python.org/dev/library/os.path.html. It takes care of the os.sep for you and makes your code more portable (not that ArcPy is portable, but in general).

for fc in fcList:
    arcpy.Copy_management(
        fc, os.path.join("d:/base/output.gdb", fc.rstrip(".shp")))
1
  • This is a good point. Using os.path.join() is a better method for building paths. May 19, 2011 at 15:55
4

try this...

In your favourite python environment type:

import os
print os.sep

Your output will be '\'or '/' depending on the operating system you're using.

@artwork21's answer is more complete ;) and beat me to the punch line!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.