我正在MacOS中编程并使用Python + Django。我必须在我们的专用网络(Windows网络)中获取一些文件并将它们移动到我们的服务器。在那里,Python / Django将读取这些文件并将数据保存在数据库中。我怎么能这样做?
我尝试了什么
source_path = "smb://server-name/GRUPOS/TECNOLOGIA_INFORMACAO/Dashboard Diretoria/"
dest_path = "./static/reports/". # This is my static folder where I want to move the file
file_name = "general_reports.csv"
shutil.copyfile(source_path + file_name, dest_path + file_name)
它给出了以下错误:
[Errno 2]没有这样的文件或目录: “SMB://服务器名称/ GRUPOS / TECNOLOGIA_INFORMACAO /仪表板 Diretoria / general_reports.csv'
这条路径(source_path)我刚从Finder复制过来,所以...我认为这是正确的。我已经在StackOverflow上搜索了,我已经尝试过其他方法,例如在路径前放置“r”......没什么......
使用的技术
感谢您的帮助和耐心。
答案 0 :(得分:1)
您需要为python导入SMB客户端库,或者在您工作之前安装该驱动器
答案 1 :(得分:1)
首先,谢谢@Van de Wack。
这是完整的解决方案:
安装pysmb库(https://pypi.org/project/pysmb/):
pip install pysmb
将库导入您的代码:
来自smb.SMBConnection导入SMBConnection
以下代码是列出所有目录的示例:
server_ip = "10.110.10.10" # Take your server IP - I have put a fake IP :)
server_name = 'myserver' # The servername for the IP above
share_name = "GRUPOS" # This is the principal folder of your network that you want's to connect
network_username = 'myuser' # This is your network username
network_password = '***' # This is your network password
machine_name = 'myuser@mac-mc70006405' # Your machine name
conn = SMBConnection(network_username, network_password, machine_name, server_name, use_ntlm_v2 = True)
assert conn.connect(server_ip, 139)
files = conn.listPath(share_name, "/TECNOLOGIA_INFORMACAO/Dashboard Diretoria/")
for item in files:
print(item.filename)