简体   繁体   中英

Downloading a file with a wildcard from Sharepoint URL with Python

I'm not sure if this can be done or not but I thought I'd ask!

I'm running a windows 10 PC using Python 2.7. I'm wanting to download a file form sharepoint to a folder on my C: drive.

OpenPath = "https://office.test.com/sites/Rollers/New improved files/"
OpenFile = "ABC UK.xlsb"

The downside is the file is uploaded externally & due to human error it can be saved as a .xlsx or ABC_UK. Therefor I only want to use the first 3 characters with a wildcard (ABC*) to open that file. Thankfully the first 3 Characters are unique & there only be one file in the path that should match.

to find the file in your dir:

import os, requests, fnmatch
#loop over dir
for filename in os.listdir(OpenPath):
    #find the file
    if fnmatch.fnmatch(filename, 'ABC_UK*'):
        #download the file
        # open file handler
        with open('C:\dwnlfile.xls', 'wb') as fh:
            #try to get it 
            result = requests.get(OpenPath+filename)
            #check u got it
            if not result.ok:
                print result.reason # or text
                exit(1)
            #save it 

            fh.write(result.content)
            print 'got it and saved'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM