-
-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathreplace.py
More file actions
34 lines (24 loc) · 1.04 KB
/
Copy pathreplace.py
File metadata and controls
34 lines (24 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
Demonstrates how to replace file content
"""
import argparse
from office365.sharepoint.client_context import ClientContext
from tests.settings import client_id, password, site_url, tenant, username
def main():
parser = argparse.ArgumentParser(description="Replace file content")
parser.add_argument("--path", default="../../data/report.csv", help="path to the local file")
args = parser.parse_args()
ctx = ClientContext(site_url).with_username_and_password(
tenant=tenant, client_id=client_id, username=username, password=password
)
print("Uploading a new file...")
with open(args.path, "rb") as f:
target_file = ctx.web.default_document_library().root_folder.files.upload(f).execute_query()
print("Replacing file content...")
with open(args.path, "rb") as content_file:
file_content = content_file.read()
target_file.save_binary_stream(file_content).execute_query()
print("Cleaning up resources...")
target_file.delete_object().execute_query()
if __name__ == "__main__":
main()