vgrem / Office365-REST-Python-Client

Microsoft 365 & Microsoft Graph Library for Python
MIT License
1.34k stars 335 forks source link

TypeError: stat: path should be string, bytes, os.PathLike or integer, not BufferedReader #610

Open timepasser69 opened 1 year ago

timepasser69 commented 1 year ago

Here is my code: target_url = "/sites/pm_files_dnb/Shared Documents" target_folder = ctx.web.get_folder_by_server_relative_url(target_url) size_chunk = 1000000

local_path = "../../../tests/data/big_buck_bunny.mp4"

local_path = r"C:\Users\ealsmar\output\testFolder\EUTRANCELLFDD_FLEX_DAY_2022-11-29.csv"

def print_upload_progress(offset): file_size = os.path.getsize(local_path) print("Uploaded '{0}' bytes from '{1}'...[{2}%]".format(offset, file_size, round(offset / file_size * 100, 2)))

with open(local_path, 'rb') as f: uploaded_file = target_folder.files.create_upload_session(f, size_chunk, print_upload_progress).execute_query()

uploaded_file = target_folder.files.create_upload_session(local_path, size_chunk, print_upload_progress).execute_query()

print('File {0} has been uploaded successfully'.format(uploaded_file.serverRelativeUrl))

I get the below error TypeError: stat: path should be string, bytes, os.PathLike or integer, not BufferedReader

stopwind93 commented 8 months ago

hi @vgrem, I am facing this issue as well, can I get your help? Thanks!

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

app_settings = {
'url': '<url>',
'client_id': '<clientID>',
'client_secret': '<clientSecret>',
}

context_auth = AuthenticationContext(app_settings['url'])
context_auth.acquire_token_for_app(client_id=app_settings['client_id'], client_secret=app_settings['client_secret'])
ctx = ClientContext(app_settings['url'], context_auth)

target_url = "<sharepoint>"
target_folder = ctx.web.get_folder_by_server_relative_url(target_url)
size_chunk = 1000000
local_path = "<file>"

def print_upload_progress(offset):
    file_size = os.path.getsize(local_path)
    print("Uploaded '{0}' bytes from '{1}'...[{2}%]".format(offset, file_size, round(offset / file_size * 100, 2)))

with open(local_path, 'rb') as f:
    uploaded_file = target_folder.files.create_upload_session(f, size_chunk,print_upload_progress).execute_query()

print('File {0} has been uploaded successfully'.format(uploaded_file.serverRelativeUrl))

image

toomax10 commented 8 months ago
def print_upload_progress(offset):
    file_size = os.path.getsize(local_path)
    print( "Uploaded '{0}' bytes from '{1}'...[{2}%]".format( offset, file_size, round( offset / file_size * 100, 2 ) ) )

# UPLOADING a fat FILE :
targetFolder = "Documentos compartidos/oneDrive/TRIPLES"
folder = ctx.web.get_folder_by_server_relative_url(targetFolder)
#folder = ctx.web.lists.get_by_title("Documents").rootFolder

size_chunk = 4000000
local_path = "Skype-8.97.0.404.exe"
print('upLoading a Skype : ')

with open( local_path, "rb") as f:
    upload_file = folder.files.create_upload_session( f, size_chunk, print_upload_progress ).execute_query()

print( "File {0} has been uploaded successfully".format( upload_file.serverRelativeUrl ) )

Produces:

upLoading a Skype :
Uploaded '0' bytes from '89146664'...[0.0%]
Uploaded '4000000' bytes from '89146664'...[4.49%]
Uploaded '8000000' bytes from '89146664'...[8.97%]
Uploaded '12000000' bytes from '89146664'...[13.46%]
Uploaded '16000000' bytes from '89146664'...[17.95%]
Uploaded '20000000' bytes from '89146664'...[22.43%]
Uploaded '24000000' bytes from '89146664'...[26.92%]
Uploaded '28000000' bytes from '89146664'...[31.41%]
Uploaded '32000000' bytes from '89146664'...[35.9%]
Uploaded '36000000' bytes from '89146664'...[40.38%]
Uploaded '40000000' bytes from '89146664'...[44.87%]
Uploaded '44000000' bytes from '89146664'...[49.36%]
Uploaded '48000000' bytes from '89146664'...[53.84%]
Uploaded '52000000' bytes from '89146664'...[58.33%]
Uploaded '56000000' bytes from '89146664'...[62.82%]
Uploaded '60000000' bytes from '89146664'...[67.3%]
Uploaded '64000000' bytes from '89146664'...[71.79%]
Uploaded '68000000' bytes from '89146664'...[76.28%]
Uploaded '72000000' bytes from '89146664'...[80.77%]
Uploaded '76000000' bytes from '89146664'...[85.25%]
Uploaded '80000000' bytes from '89146664'...[89.74%]
Uploaded '84000000' bytes from '89146664'...[94.23%]
Uploaded '88000000' bytes from '89146664'...[98.71%]
Uploaded '89146664' bytes from '89146664'...[100.0%]
File /sites/[mySP-site]/Documentos compartidos/oneDrive/TRIPLES/Skype-8.97.0.404.exe has been uploaded successfully

Perhaps, your file is too short to reach 1,000,000 bytes or your file has a missing header ...

Good Luck !!!

stopwind93 commented 8 months ago

hi @toomax10, thanks for spending your time to help. I have reduced the size_chunk but still facing this error.

I am trying to upload a csv file. How do I check whether my file is missing a header?

toomax10 commented 8 months ago

Ummm . ... Sorry .csv is just text, does not have header or tail.... [ just BOF and EOF ] So, then it is probably you are working in win environment with "\\" as folders separators [they must be echoed], so may be you test setting the .CSV file in the same path of py code file, or visconverse, setting your py code file in the .csv path.

stopwind93 commented 8 months ago

i am working on linux environment. setting the csv file in the same path of py code still giving the same error

stopwind93 commented 8 months ago

found the solution

with open(local_path, 'rb') as f:
    uploaded_file = target_folder.files.create_upload_session(f, size_chunk,print_upload_progress).execute_query()

change to uploaded_file = target_folder.files.create_upload_session(local_path, size_chunk,print_upload_progress).execute_query()