need method to split filenames for item titles in item add()

05-16-2018 01:53 PM
Occasional Contributor III

probably a newbie question but here goes:

I am adding a large number of PDF's to a newly created folder...

I am using glob.glob to grab all file paths into a list

I also need to use the data_properties dictionary to add the filename as the items title and not the entire path name.

code is below... everything works but its uploading the title as the full path name, i need to split the file path to only grab the files name for title.

(before you ask, if I don't use time.sleep the API will skip some items for the sharing between function calls.)

method to grab filenames within the for loop and assign to variable to use in the pdf_properties 'title'

import arcgis
from arcgis.gis import *
import os
import glob
import time

gis = GIS(None, "username", "password")
contentManager = arcgis.gis.ContentManager(gis)
contentManager.create_folder('new folder name,None)

pdf_list = glob.glob(r'C:\Users\username\Desktop\pdf\*.pdf')

additem = arcgis.gis.ContentManager(gis)

for pdf in pdf_list:
    pdf_properties = {'type':'PDF','title':pdf,'tags':'pdf, arcgis,lakeworth', 'access':'Public'}
    additem.add(pdf_properties,pdf,folder='Historic PDFs')
item_search ='', item_type='PDF')

for item in item_search:

0 Kudos
4 Replies
MVP Esteemed Contributor

11.2. os.path — Common pathname manipulations — Python 3.6.5 documentation , see split or basename

pdf_name = os.path.basename(pdf)
Occasional Contributor III

yes I got the answer on stackoverflow but this still appends the ".pdf" suffix

I was able to use


to get the desired results


0 Kudos
MVP Esteemed Contributor

You might want to use os.path.splitext(), just in case a PDF file name has a period in it, which is allowed.

Occasional Contributor III

all the pdf names will integers (parcel control numbers) but thanks for the info it will be helpful with other projects

0 Kudos