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?
Asked
Active
Viewed 182 times
0
-
what have you tried so far and where are you stuck please ? – D.L Dec 08 '22 at 09:21
-
I dont know how to do it so I cant try because Iam very new to programming. – Leon48912 Dec 08 '22 at 10:47
-
The question has been asked before here: https://stackoverflow.com/questions/23696705/how-to-upload-a-file-to-sharepoint-site-using-python-script ... have you tried this ? – D.L Dec 08 '22 at 10:50
-
Does this answer your question? [How to upload a file to sharepoint site using python script](https://stackoverflow.com/questions/23696705/how-to-upload-a-file-to-sharepoint-site-using-python-script) – D.L Dec 08 '22 at 10:51
-
I have voted to close this question as it has been asked before and no attempt has been made. – D.L Dec 08 '22 at 10:52
-
I tried this but its not working for me. I dont have the knowledge to transfer this. I need more explanation. @D.L thank you for nothing. Do you never was a beginner in Programming? FY – Leon48912 Dec 08 '22 at 11:15
-
I tried both of this and it didnt work and I dont know what happening there – Leon48912 Dec 08 '22 at 11:16
1 Answers
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.
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()```

Saleem Shaik
- 16
- 1
-
Thank you but this didnt work. Becasue the module dont getting support – Leon48912 Jan 12 '23 at 12:59