0

I have a test.txt on my Desktop and now I want to upload it to a Sharepoint Directory via Python3. How can I do that?

7
  • what have you tried so far and where are you stuck please ? Commented Dec 8, 2022 at 9:21
  • I dont know how to do it so I cant try because Iam very new to programming. Commented Dec 8, 2022 at 10:47
  • The question has been asked before here: stackoverflow.com/questions/23696705/… ... have you tried this ? Commented Dec 8, 2022 at 10:50
  • Does this answer your question? How to upload a file to sharepoint site using python script Commented Dec 8, 2022 at 10:51
  • I have voted to close this question as it has been asked before and no attempt has been made. Commented Dec 8, 2022 at 10:52

1 Answer 1

0

I'll start by saying this example is adapted from the example for Office365-REST-Python-Client. It works with sharepoint online using the rest api.

https://github.com/vgrem/Office365-REST-Python-Client/blob/master/examples/sharepoint/files/upload_file.py

Example url you might want to upload to [baseurl][site][folder][file]. https://your_company.sharepoint.com/path/to/site/Shared Documents/file.txt

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext

baseurl = 'https://your_company.sharepoint.com'
basesite = '/path/to/site' # every share point has a home.
siteurl = baseurl + basesite 

localpath = ./file.txt
remotepath = Shared Documents/file.txt # existing folder path under sharepoint site.

ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username, password)
ctx = ClientContext(siteurl, ctx_auth) # make sure you auth to the siteurl.

with open(localpath, 'rb') as content_file:
    file_content = content_file.read()

dir, name = os.path.split(remotepath)
file = ctx.web.get_folder_by_server_relative_url(dir).upload_file(name, file_content).execute_query()```
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you but this didnt work. Becasue the module dont getting support

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.