如何从共享点上的给定文件夹中读取文件名

时间:2019-06-28 07:34:53

标签: python rest api sharepoint

我正在用python 2.7编写一个可以自动执行ETL的工具,该工具将在循环中读取sharepoint中的文件并将其转换为数据帧。我的ETL已完成,但是我无法从可以提供我的ETL的共享点上的文件夹中读取文件名。任何帮助表示赞赏。预先感谢

我已经检查了context.web的类成员,并且可以看到get_folder_by_server_relative_url

import os

from office365.sharepoint.caml_query import CamlQuery
from office365.runtime.auth.authentication_context import 
AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File
from office365.sharepoint.file_creation_information import 
FileCreationInformation
import inspect


url= "https://XXX.sharepoint.com/sites/XXX/"
username = "blah@xxx.com.au"
password = "blah123!"

ctx_auth = AuthenticationContext(url=url)
if ctx_auth.acquire_token_for_user(username=username,password=password):
 context = ClientContext(url, ctx_auth)
 print(inspect.getmembers(context.web))

 lists = context.web.get_folder_by_server_relative_url('Shared Documents/ELZ/')
 items = lists.Folders
 context.Load(items)
 context.ExecuteQuery()


 folder = GetListItemFolder(item)
 for item in items:
   print "File name: {0}".format(item.properties["Name"])

AttributeError:“ FolderCollection”对象没有属性“ get_folder_by_server_relative_url”

3 个答案:

答案 0 :(得分:0)

检查此示例read_folder_and_files_alt

lists = context.web.lists.get_by_title('Documents')
qry = CamlQuery.create_all_items_query()
items = list_obj.get_items(qry)
context.load(items)
context.execute_query()
for cur_item in items:
        print("File name: {0}".format(cur_item.properties["Title"]))

答案 1 :(得分:0)

我终于解决了,下面是正确的代码。

ctx_auth = AuthenticationContext(url=url)  
if ctx_auth.acquire_token_for_user(username=username,password=password):
     context = ClientContext(url, ctx_auth)  
     folder_obj = context.web.get_folder_by_server_relative_url('Shared 
     Documents/ELZ/')  
     files = folder_obj.files.order_by('TimeLastModified')    
     context.load(files)  
     context.execute_query()   
     for file in files:  
         print "File name: {0}".format(file.properties['Name'])

答案 2 :(得分:0)