2016-06-14 39 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

也没有模块命名请求。 –