[英]Accessing shared smb ubuntu in python scripts
I have a shared ubuntu drive on my network that I can access in nautilus using smb://servername/sharedfolder or smb:///sharedfolder.我的网络上有一个共享的 ubuntu 驱动器,我可以使用 smb://servername/sharedfolder 或 smb:///sharedfolder 在 nautilus 中访问它。
I need to be able to access it python scripting from my ubuntu machine (8.10), but I can't figure out how.我需要能够从我的 ubuntu 机器(8.10)访问它的 python 脚本,但我不知道如何。 I tried the obvious way (same address as nautilus), but wasn't successful.
我尝试了明显的方法(与鹦鹉螺相同的地址),但没有成功。
To play around, I tried printing the content of that folder with both:为了玩耍,我尝试使用以下两种方式打印该文件夹的内容:
Code: for file in os.listdir("smb://servername/sharedfolder") print file Both give me "no file or directory" error on that path.代码:for file in os.listdir("smb://servername/sharedfolder") print file 在该路径上都给我“没有文件或目录”错误。
I'd really appreciate help on this - thanks.我真的很感激这方面的帮助 - 谢谢。
Python can only handle local paths. Python 只能处理本地路径。 Samba is a remote path read by a driver or application in your Linux system and can there for not be directly accessed from Python unless you're using a custom library like this experimental library .
Samba 是您的 Linux 系统中的驱动程序或应用程序读取的远程路径,除非您使用像这个实验库这样的自定义库,否则不能直接从 Python 访问。
You could do something similar to (make sure your user has the permission needed to mount stuff):您可以执行类似的操作(确保您的用户具有挂载所需的权限):
import os
from subprocess import Popen, PIPE, STDOUT
# Note: Try with shell=True should not be used, but it increases readability to new users from my years of teaching people Python.
process = Popen('mkdir ~/mnt && mount -t cifs //myserver_ip_address/myshare ~/mnt -o username=samb_user,noexec', shell=True, stdout=PIPE, stderr=PIPE)
while process.poll() is None:
print(process.stdout.readline()) # For debugging purposes, in case it asks for password or anything.
print(os.listdir('~/mnt'))
Again, using shell=True
is dangerous, it should be False
and you should pass the command-string as a list.同样,使用
shell=True
是危险的,它应该是False
并且您应该将命令字符串作为列表传递。 But for some reason it appears "complex" if you use it the way you're supposed to, so i'll write you this warning and you can choose to follow common guidelines or just use this to try the functionality out.但是由于某种原因,如果你按照你应该的方式使用它,它会显得“复杂”,所以我会写给你这个警告,你可以选择遵循通用指南,或者只是使用它来尝试功能。
Here's a complete guide on how to manually mount samba .这是有关如何手动挂载 samba 的完整指南。 Follow this, and replace the manual steps with automated programming.
遵循这一点,并用自动编程代替手动步骤。
Python's file-handling functions like os.listdir
don't take GNOME URLs like Nautilus does, they take filenames. Python 的文件处理函数(如
os.listdir
)不像 Nautilus 那样采用 GNOME URL,它们采用文件名。 Those aren't the same thing.这些不是一回事。
You have three basic options here:您在这里有三个基本选项:
I have mainly used the answer from "Torxed" with somes modifications:我主要使用了“Torxed”的答案并进行了一些修改:
import os, time
from subprocess import Popen, PIPE, STDOUT
process = Popen('sudo mount -t cifs //192.168.1.1/Raspi /home/pi/mnt -o vers=1.0,username=pi,password=YOUR_PASSWORD', shell=True, stdout=PIPE, stderr=PIPE)
while process.poll() is None:
print(process.stdout.readline()) # For debugging purposes,
# the following command will list the content of the folder
# thus be sure to have files or folders in the shared folder to check
# for a correct output
print(os.listdir('/home/pi/mnt'))
time.sleep(2)
process = Popen('sudo umount //192.168.1.1/Raspi', shell=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.