/
file-management.py
35 lines (25 loc) · 1.16 KB
/
file-management.py
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
35
from smbclient import link, open_file, register_session, remove, rename, stat, symlink
# Optional - register the server with explicit credentials
register_session("server", username="admin", password="pass")
# Read an existing file as text (credentials only needed for the first request to the server if not registered.)
with open_file(r"\\server\share\file.txt", username="admin", password="pass") as fd:
file_contents = fd.read()
# Read an existing file as bytes
with open_file(r"\\server\share\file.txt", mode="rb") as fd:
file_bytes = fd.read()
# Create a file and write to it
with open_file(r"\\server\share\file.txt", mode="w") as fd:
fd.write("content")
# Write data to the end of an existing file
with open_file(r"\\server\share\file.txt", mode="a") as fd:
fd.write("\ndata at the end")
# Delete a file
remove(r"\\server\share\file.txt")
# Get info about a file
stat(r"\\server\share\file.txt")
# Create a symbolic link
symlink(r"\\server\share\directory", r"\\server\share\link")
# Create a hard link
link(r"\\server\share\file.txt", r"\\server\share\hard-link.txt")
# Rename a file
rename(r"\\server\share\old-name.txt", r"\\server\share\new-name.txt")