0

Here is the python code I am trying to use to upload csv file from local to sharepoint. Can you please help with error message?

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.runtime.utilities.request_options import RequestOptions

app_settings = {
    'url': 'https://contoso.sharepoint.com/sites/ProjectName'
    'client_id': '4f510936',
    'client_secret': 'C9'
 }

ctx_auth = AuthenticationContext(url=app_settings['url'])



if ctx_auth.acquire_token_for_app(client_id=app_settings['client_id'], client_secret=app_settings['client_secret']):
    

    path = r"C:\Users\Test.csv"
    with open(path, 'rb') as content_file:
        file_content = content_file.read()

    list_title = "My Folder/Azure"
    target_folder = ctx.web.lists.get_by_title(list_title).rootFolder
    
    name = os.path.basename(path)
    print(target_folder,name)
    target_file = target_folder.upload_file(name, file_content)
    ctx.execute_query()
    print("File url: {0}".format(target_file.serverRelativeUrl))


I would like to upload the file to "Azure" folder under "My Folder" My Sharepoint location: https :// contonso .sharepoint.com/sites/ProjectName/My%20Folder/Forms/AllItems.aspx and sub folder is>>Azure

Error Message: AttributeError Traceback (most recent call last) in 21 name = os.path.basename(path) 22 print(target_folder,name) ---> 23 target_file = target_folder.upload_file(name, file_content) 24 ctx.execute_query() 25 print("File url: {0}".format(target_file.serverRelativeUrl))AttributeError: 'Folder' object has no attribute 'upload_file'

Log from module installation:

Processing c:\users\user_id\appdata\local\pip\cache\wheels\a9\22\30\e7dfb5f53fe2ad180c4c789945bf1320804c75a31ffc79cf59\office365_rest_python_client-2.1.7.post1-cp37-none-any.whl Requirement already satisfied: requests in c:\users\user_id\appdata\local\continuum\anaconda3\lib\site-packages (from Office365-REST-Python-Client) (2.24.0) Requirement already satisfied: adal in c:\users\user_id\appdata\local\continuum\anaconda3\lib\site-packages (from Office365-REST-Python-Client) (1.2.2) Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\users\user_id\appdata\local\continuum\anaconda3\lib\site-packages (from requests->Office365-REST-Python-Client) (1.25.9) Requirement already satisfied: certifi>=2017.4.17 in c:\users\user_id\appdata\local\continuum\anaconda3\lib\site-packages (from requests->Office365-REST-Python-Client) (2020.6.20) Requirement already satisfied: chardet<4,>=3.0.2 in c:\users\user_id\appdata\local\continuum\anaconda3\lib\site-packages (from requests->Office365-REST-Python-Client) (3.0.4) Requirement already satisfied: idna<3,>=2.5 in c:\users\user_id\appdata\local\continuum\anaconda3\lib\site-packages (from requests->Office365-REST-Python-Client) (2.10) Requirement already satisfied: python-dateutil>=2.1.0 in c:\users\user_id\appdata\local\continuum\anaconda3\lib\site-packages (from adal->Office365-REST-Python-Client) (2.8.1) Requirement already satisfied: cryptography>=1.1.0 in c:\users\user_id\appdata\local\continuum\anaconda3\lib\site-packages (from adal->Office365-REST-Python-Client) (2.9.2) Requirement already satisfied: PyJWT>=1.0.0 in c:\users\user_id\appdata\local\continuum\anaconda3\lib\site-packages (from adal->Office365-REST-Python-Client) (1.7.1) Requirement already satisfied: six>=1.5 in c:\users\user_id\appdata\local\continuum\anaconda3\lib\site-packages (from python-dateutil>=2.1.0->adal->Office365-REST-Python-Client) (1.15.0) Requirement already satisfied: cffi!=1.11.3,>=1.8 in c:\users\user_id\appdata\local\continuum\anaconda3\lib\site-packages (from cryptography>=1.1.0->adal->Office365-REST-Python-Client) (1.14.0) Requirement already satisfied: pycparser in c:\users\user_id\appdata\local\continuum\anaconda3\lib\site-packages (from cffi!=1.11.3,>=1.8->cryptography>=1.1.0->adal->Office365-REST-Python-Client) (2.20) Installing collected packages: Office365-REST-Python-Client Successfully installed Office365-REST-Python-Client-2.1.7.post1

Idleguys
  • 325
  • 1
  • 7
  • 18
  • target_folder is a string, not the context object. – ewokx Jul 02 '20 at 03:19
  • @ewong here is reference code and looks like it is object: https://github.com/vgrem/Office365-REST-Python-Client/blob/master/examples/sharepoint/upload_file.py – Idleguys Jul 02 '20 at 03:35
  • My bad. Sorry I seemed to have mis-processed that error. That said, there seems to be some disconnect between the github.com's office365/sharepoint/folders/folder.py code and how you're system's setup. Is your python's office365 reset module updated? – ewokx Jul 02 '20 at 03:56
  • What I mean is in your office365 rest python module in lib/site-packages, in office365/sharepoint/folders/folder.py, does it have upload_file()? Have you tried updating the office365 module? – ewokx Jul 02 '20 at 03:59
  • Thanks for your responses @ewong, folder.py is available on sharepoint folder.. I dont have "folders" on my location "AppData\Local\Continuum\anaconda3\Lib\site-packages\office365\sharepoint.folder.py" – Idleguys Jul 02 '20 at 14:29
  • Yes I have updated module from my notebook.. pip install Office365-REST-Python-Client.. Also I noticed .. there is no upload_file method on this folder.py file.. :( – Idleguys Jul 02 '20 at 14:33
  • I have tried re-installing the modules.. but still don't see upload_file() method in folder.py. Updated question with log from pip installation.. – Idleguys Jul 02 '20 at 18:38

0 Answers0