在python脚本中访问共享的smb ubuntu [英] Accessing shared smb ubuntu in python scripts

查看:322
本文介绍了在python脚本中访问共享的smb ubuntu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网络上有一个共享的ubuntu驱动器,可以使用smb:// servername / sharedfolder或smb:/// sharedfolder在nautilus中访问。

I have a shared ubuntu drive on my network that I can access in nautilus using smb://servername/sharedfolder or smb:///sharedfolder.

I需要能够从我的ubuntu机器(8.10)访问python脚本,但是我不知道如何。我尝试了一种明显的方式(地址与鹦鹉螺相同),但没有成功。

I need to be able to access it python scripting from my ubuntu machine (8.10), but I can't figure out how. 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:

代码:
用于文件os.listdir( smb:// servername / sharedfolder)
打印文件
两者都给我

Code: for file in os.listdir("smb://servername/sharedfolder") print file Both give me "no file or directory" error on that path.

我非常感谢您的帮助-谢谢。

I'd really appreciate help on this - thanks.

推荐答案

Python只能处理本地路径。
Samba是Linux系统中的驱动程序或应用程序读取的远程路径,除非您正在使用此实验库

Python can only handle local paths. 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.

您可以执行类似的操作(确保您的用户具有装载东西):

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'))

同样,使用 shell = True 是危险的,它应该为 False ,并且应将命令字符串作为列表传递。但是由于某种原因,如果您按应有的方式使用它,它会显得复杂,因此,我将向您发送此警告,并且您可以选择遵循通用准则,也可以仅使用它来尝试功能。

Again, using shell=True is dangerous, it should be False and you should pass the command-string as a list. 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.

此处是有关如何手动安装samba的完整指南。遵循此步骤,并用自动编程替换手动步骤。

Here's a complete guide on how to manually mount samba. Follow this, and replace the manual steps with automated programming.

这篇关于在python脚本中访问共享的smb ubuntu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆