In the code below, if the upload_file_2_sp function is called, I’m unsure of how the offset argument is being assigned a value.
The code will run successfully as is, but I’m trying to add a second argument to the print_upload_progress function to pass each file in my file list loop to the print_upload_progress function, so I can display the upload progress as the for loop iterates through my list of files. Not sure how to do this, because I’m not sure how the assignment of the offset argument is currently working.
local_path = '/sso_win_mounts/win_appsprod/CRMDAGROUP/phones/reporting/customer_care/prod/weekly/kpm/test 3.xlsb'
#display progress of file upload to sharepoint
def print_upload_progress(offset):
print(str(offset))
# type: (int) -> None
file_size = os.path.getsize(local_path)
print(
"Uploaded '{0}' bytes from '{1}'...[{2}%]".format(
offset, file_size, round(offset / file_size * 100, 2)
)
)
#upload files to sharepoint
def upload_file_2_sp(report_list, base_url, target_url):
ctx = sharepointConnection(base_url)
target_folder = ctx.web.get_folder_by_server_relative_url(target_url)
size_chunk = 1000000
for file in report_list:
uploaded_file = target_folder.files.create_upload_session(
file, size_chunk, print_upload_progress
).execute_query()
print("File {0} has been uploaded successfully".format(uploaded_file.serverRelativeUrl))
>Solution :
The arguments are passed automatically by create_upload_session(), you can’t change what it passes.
You could use a lambda to pass additional arguments to your function.
for file in report_list:
uploaded_file = target_folder.files.create_upload_session(
file, size_chunk,
lambda offset, file=file: print_upload_progress(offset, file)
).execute_query()
print("File {0} has been uploaded successfully".format(uploaded_file.serverRelativeUrl))
See Creating functions (or lambdas) in a loop (or comprehension) for the explanation of the file=file argument to the lambda.