问题描述
我正在尝试通过python将smb网络共享挂载到桌面上,我不希望将该共享挂载在一个文件夹中,但是所有其他已挂载的共享都是(如果我使用连接到服务器" OSX我希望将python挂载安装在同一位置).这是当前的python代码:
I am trying to mount a smb network share onto the desktop via python, I don't want the share to be mounted in a folder, but were all the other mounted shares are (if I use 'connect to Server' in OSX I want my python mount to be mounted in the same location).Here is the current python code:
directory = os.path.expanduser('~/Desktop')
directory = os.path.normpath(directory)
os.system("mount_smbfs //server/servershare " + directory)
当我执行以上操作时,会发生一些奇怪的事情.在取景器中,我的房子有一个房子的图标,我的用户名更改为安装名称,将它弄乱了.
When I run the above, something strange happens. In finder, my home, which has the icon of a house and my username changes to the mount name, it screws it up a bit.
推荐答案
如果您希望通过类似Finder的方式进行此操作,请通过Python通过Shell在AppleScript中进行操作:
If you want to do this this the kosher, Finder-like way, do it in AppleScript via shell via Python:
os.system("osascript -e 'mount volume \"smb://server/servershare\"'")
您不需要其他任何东西-没有挂载点.这与选择连接到服务器"相同,并且所产生的音量将按预期显示在/Volumes中.
You don't need anything else -- there's no mount point. This is identical to choosing "Connect To Server", and the resulting volume will show up in /Volumes as expected.
如果您需要指定用户名和/或密码,可以这样做:
If you need to specify a username and/or password, you can do so:
os.system("osascript -e 'mount volume \"smb://server/servershare\" \
as user name \"myUserName\" with password \"myPassword\"'")
如果您想使用mount_smbfs
的原始方式进行操作,我想您希望directory
成为在/Volumes
中创建的文件夹,例如/Volumes/mySmbVolume
,尽管我从未尝试过这样做.编写完成后,您将用要安装的卷替换实际的Desktop文件夹.但是,您可以在Desktop中创建一个文件夹并将其用于directory
,它可能会起作用.但是,我会像我用Mac的一般做事方式将其编写为最标准的方式来做到这一点.
If you want to do it your original way using mount_smbfs
, I think you want directory
to be a folder you create in /Volumes
, e.g. /Volumes/mySmbVolume
, though I've never tried to do it this way. As you have it written, you're replacing your actual Desktop folder with the volume you're mounting. You could, however, make a folder inside Desktop for and use that for directory
, and it might work. However, I'd do it like I wrote it to be most standard with the usual Mac way of doing things.
这篇关于在桌面上挂载SMB网络共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!