2

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?

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Can you show an example of this method in code?
If my code does the job, mark my solution

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.