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?
-
what have you tried so far and where are you stuck please ?darren– darren2022-12-08 09:21:08 +00:00Commented Dec 8, 2022 at 9:21
-
I dont know how to do it so I cant try because Iam very new to programming.Leon48912– Leon489122022-12-08 10:47:29 +00:00Commented Dec 8, 2022 at 10:47
-
The question has been asked before here: stackoverflow.com/questions/23696705/… ... have you tried this ?darren– darren2022-12-08 10:50:54 +00:00Commented Dec 8, 2022 at 10:50
-
Does this answer your question? How to upload a file to sharepoint site using python scriptdarren– darren2022-12-08 10:51:16 +00:00Commented 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.darren– darren2022-12-08 10:52:51 +00:00Commented Dec 8, 2022 at 10:52
|
Show 2 more comments
1 Answer
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.
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()```
1 Comment
Leon48912
Thank you but this didnt work. Becasue the module dont getting support