1

I'm supposed to publish each file as a uniquely names feature service, but despite my approach I keep having issues where my csv files aren't found... a friend advised me to add my initials to each CSV because sometimes arcgis doesn't like when the names aren't unique enough, but I keep getting the same errors. any ideas?

from arcgis.gis import GIS
import os
import pandas as pd
from arcgis.gis import GIS
from arcgis.features.analysis import *
from arcgis.features import FeatureLayer

gis = GIS("http://slustl.maps.arcgis.com/home", "SLysy_SLUSTL")

data_dir = r"C:\Users\Stephanie\NOTEBOOKS\FINAL\FSI"
file_list = os.listdir(data_dir)

csv_file_list = [x for x in file_list if x.endswith(".csv")]
print("Number of .csv files found: " + str(len(csv_file_list)))

for csv in csv_file_list:
    item = gis.content.add({}, data_dir + csv)   # .sd file is uploaded and a .sd file item is created
    published_item = item.publish()   

Error

RuntimeError: File(C:\Users\Stephanie\NOTEBOOKS\FINAL\FSIFSI-2006sl.csv) not found.

Print output

['FSI-2006sl.csv',
 'FSI-2007sl.csv',
 'FSI-2008sl.csv',
 'FSI-2009sl.csv',
 'FSI-2010sl.csv',
 'FSI-2011sl.csv',
 'FSI-2012sl.csv',
 'FSI-2013sl.csv',
 'FSI-2014sl.csv',
 'FSI-2015sl.csv',
 'FSI-2016sl.csv',
 'FSI-2017sl.csv',
 'FSI-2018sl.csv']
  • csv_file_list = [os.path.join(data_dir,x) for x in file_list if x.endswith(".csv")]. Or is each csv name starting with FSI-? Then remove \FSI from data_dir – BERA May 4 at 5:29
2

The path you're building up is probably NOT correct per:

data_dir = r"C:\Users\Stephanie\NOTEBOOKS\FINAL\FSI"
item = gis.content.add({}, data_dir + csv) 

I'd expect this to create a path like: C:\Users\Stephanie\NOTEBOOKS\FINAL\FSImycsv.csv (note the lack of \ separator between path and file name) I'd suggest doing your path like: item = gis.content.add({}, os.path.join(data_dir, csv))

Alternatively, you may want to consider glob, as it handles a lot of the listing of files for you. For example:

import glob
csv_list = [f for f in glob.glob("c:/temp/*.csv")]       
csv_list
['c:/temp\\zzzz.csv', 'c:/temp\\aaaaa.csv', 'c:/temp\\bbbb.csv', 'c:/temp\\foo.csv']

Or better yet, just let glob run your loop and skip building the list.

for f in glob.glob("c:/temp/*.csv"):
   item = gis.content.add({}, f)   
   published_item = item.publish()  
| improve this answer | |

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.