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

asked Nov 6, 2023 at 22:24
5
  • Always use os.path to manipulate paths. Commented Nov 6, 2023 at 23:13
  • See what __file__ points to (I'm not sure in the case of a *.pyt) Commented Nov 7, 2023 at 0:50
  • I receive errors What errors? Please include all relevant information in your question. 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` 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.

answered Nov 7, 2023 at 2:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.