Reputation: 105
I am trying to create a python script that imports files from a desktop folder to a Sharepoint sites folder.
I am using Sharepy to perform this task.
This is the script I have currently:
import sharepy
import os
import glob
s = sharepy.connect("https://xxx.sharepoint.com", username='[email protected]', password='xxx')
filesToUpload = (glob.glob("C:/Users/xxx/Desktop/xxx/*.xlsx"))
for fileToUpload in filesToUpload:
headers = {"accept": "application/json;odata=verbose",
"content-type": "application/x-www-urlencoded; charset=UTF-8"}
with open(fileToUpload, 'rb') as read_file:
content = read_file.read()
p = s.post(f"https://xxx.sharepoint.com/xxx/yyy/_api/web\
/GetFolderByServerRelativeUrl('/xxx/yyy/Shared Documents/zzz/aaa/')/Files/add(url='fileToUpload',overwrite=true)", data=content, headers=headers)
It works fine when I do it one at a time but not with this loop. I have about 5 xlsx excel sheets in my folder currently.
How can I upload multiple files with a single request?
Upvotes: 2
Views: 2413
Reputation: 113
Can you try to put the files to import into a list which you will loop over?
Try this :
import os
import glob
s = sharepy.connect("https://xxx.sharepoint.com", username='[email protected]', password='xxx')
filesToUpload = [f for f in glob.glob("*.txt")] #New line
for fileToUpload in filesToUpload:
headers = {"accept": "application/json;odata=verbose",
"content-type": "application/x-www-urlencoded; charset=UTF-8"}
with open(fileToUpload, 'rb') as read_file:
content = read_file.read()
p = s.post(f"https://xxx.sharepoint.com/xxx/yyy/_api/web\
/GetFolderByServerRelativeUrl('/xxx/yyy/Shared Documents/zzz/aaa/')/Files/add(url='fileToUpload',overwrite=true)", data=content, headers=headers)
Upvotes: 3