1

I have a CSV file in my toolbox folder. I would like to import the CSV file into my arcpy script to run in conjunction with my arcpy script.

Folder structure:

  • src
    • data
      • my_csv.csv
    • mytoolbox.pyt

How would I import the my_csv.csv into the mytoolbox.pyt?

I receive an error when I try running through ArcGIS Pro GUI.: FileNotFoundError: [Errno 2] No such file or directory: ../data/my_csv.csv

code:


    def execute(self, parameters, messages):
        """The source code of the tool."""
        # Open layer and process!!
        arcpy.AddMessage(os.getcwd()) # output is AppData folder for windows
        df_cat_codes = pd.read_csv("../data/my_csv.csv")
        arcpy.AddMessage("CSV shape: ", df_cat_codes.shape)

        return

I am using the template from: https://pro.arcgis.com/en/pro-app/latest/arcpy/geoprocessing_and_python/a-template-for-python-toolboxes.htm

5
  • Always use os.path to manipulate paths.
    – Vince
    Commented Nov 6, 2023 at 23:13
  • See what __file__ points to (I'm not sure in the case of a *.pyt)
    – mikewatt
    Commented Nov 7, 2023 at 0:50
  • I receive errors What errors? Please include all relevant information in your question.
    – Berend
    Commented Nov 7, 2023 at 10:17
  • Isn't the relative path to the csv just data/my_csv.csv? According to your bullet points, mytoolbox.pyt and data are at the same level. Commented Nov 7, 2023 at 12:10
  • When I check the current location using os.getcwd() (after adding toolbox in Arc Pro), the path join when running in Arc Pro is something like `.\AppData\Local\Temp\ArcGISProTemp82956`
    – Clouseau
    Commented Nov 7, 2023 at 17:06

1 Answer 1

4

This:

scriptsPath=os.path.dirname(os.path.realpath(__file__))

will give you path to toolbox (works with tbx), so join 'data' and scriptsPath to get location of csv file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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