Select to view content in your preferred language

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

1182
4
05-16-2018 01:53 PM
Lake_Worth_BeachAdmin
Frequent Contributor

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')
    time.sleep(1)
item_search = gis.content.search(query='', item_type='PDF')
item_search

for item in item_search:
    item.share(everyone=True)
    time.sleep(1)

0 Kudos
4 Replies
JoshuaBixby
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)
Lake_Worth_BeachAdmin
Frequent Contributor

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

I was able to use

pdf_name.split(".")[0]

to get the desired results

thanks!

0 Kudos
JoshuaBixby
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.

Lake_Worth_BeachAdmin
Frequent Contributor

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

0 Kudos