2

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!

4

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")
  • 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 Stimson Aug 14 '14 at 21:39

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.