Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Why is it that when I label my directories this way:

arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:\\Users\\Documents\\ArcGIS\\Testing"
arcpy.env.scratchWorkspace = "C:\\Users\Documents\\ArcGIS\\Testing"
scratch = "C:\\Users\Documents\\ArcGIS\\Testing"
space = "C:\\Users\\Documents\\ArcGIS" #General Directory
workspace = "C:\\Users\\Documents\\ArcGIS\\Textfiles" #Where the final product is saved
folder = "C:\\Users\\Documents\\ArcGIS\\AQS\\site_coordinates\\" #Location of the the site data
textfile_place = "C:\\Users\\Documents\\ArcGIS\\Textfiles\\pollution.txt" #Final Textfile
data = "C:\\Users\\Documents\\ArcGIS\\Textfiles\\data.txt" #Location of data

it reads all of my data, but when I try to clean it up and make it more transferable across computers like this:

arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:\\Users\\Documents\\ArcGIS\\Testing"
arcpy.env.scratchWorkspace = "C:\\Users\\Documents\\ArcGIS\\Testing"
space = "C:\\Users\\Documents\\ArcGIS"
scratch = space + "\\Testing"
workspace = space + "\\Textfiles"
textfile_place = workspace + "\\pollution.txt"
data = workspace + "data.txt"
folder = space + "\\AQS\\site_coordinates\\"

it doesn't work and says the files don't exist? I've been trying to look on the web but can't find a solution to the problem. Thanks!

share|improve this question

1 Answer 1

up vote 4 down vote accepted

The os Python module will help you solve this problem. For example:

import os, arcpy

arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:\\Users\\Documents\\ArcGIS\\Testing"
arcpy.env.scratchWorkspace = "C:\\Users\\Documents\\ArcGIS\\Testing"
space = "C:\\Users\\Documents\\ArcGIS"

scratch = os.path.join(space, "Testing")
workspace = os.path.join(space, "Textfiles")
textfile_place = os.path.join(workspace, "pollution.txt")
data = os.path.join(workspace, "data.txt")
folder = os.path.join(space, "AQS", "site_coordinates")
share|improve this answer
    
That works, thanks! –  Alex Aug 14 at 20:02
1  
Nice answer Aaron, simple and concise. Alex, something else that may help is os.path.exists(path) to check for an object in your filesystem, this will return true if a file or folder matches path - couple with os.path.isdir(path) to confirm if a folder exists and then use os.mkdirs(path) to create the folder if you can't find it. –  Michael Miles-Stimson Aug 14 at 21:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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