2016-06-14 70 views
1

我不確定這是否可以完成,但我想我會問!用Python從Sharepoint URL下載帶有通配符的文件

我使用Python 2.7運行Windows 10 PC。我想要將文件格式的Sharepoint下載到我的C:驅動器上的文件夾中。

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

缺點是該文件是由於人爲錯誤它可以被保存爲一個的.xlsx或ABC_UK外部&上傳。因此,我只想使用通配符(ABC *)的前3個字符來打開該文件。謝天謝地,前3個字符是獨一無二的&只有一個文件應該匹配。

回答

0

找到你的目錄文件:

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' 
+0

看起來不錯,但我得到一個錯誤 (***外機能的研究「迴歸」) –

+0

所以把它放在一個函數或更換回與「打印」退出「byebye'」 –

+0

也沒有模塊命名請求。 –