3

I am using glob to get a list of all PDF files in a folder (I need full path names to upload file to cloud)

also, during the upload I need to assign a "title" to the file which we be the items name in the cloud.

I need to split the last "\" and the "." and get the values in between. for example:

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

a item in the list will be: "c:\User\username\Desktop\pdf\4434343434331.pdf"

I need another pythonic way to grab the pdfs file name in a separate variable while still in the for loop.

for file in pdf_list:
    upload.file
    file.title(file.split(".")[0]

however the above split will not return my desired results but something along those lines

I am using a for loop to upload each pdf (using file path)

2 Answers 2

4

Actually, there is a function for this already:

for file in pdf_list:
   file_name = os.path.basename(file)
   upload.file(file_name)
Sign up to request clarification or add additional context in comments.

1 Comment

this worked but left the ".pdf" suffix. I was able to use file_name.split(".")[0] to get the desired results. thanks!
1

You can use pathlib, for example:

from pathlib import Path
p = list(Path('C:/User/username/Desktop/pdf').glob('*.pdf'))
first_filename = p[0].name

Comments

Your Answer

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.