【问题标题】:Mounting disk-drive using Python使用 Python 挂载磁盘驱动器
【发布时间】:2014-12-23 08:43:16
【问题描述】:

我正在尝试编写一个安装并打开我的文件夹的 Python 脚本。 但我有“找不到”/media/New Volume”。”错误。 如果我首先安装并运行脚本,它可以工作,打开我的新卷。 但是如果我不安装并运行它,我会得到错误。

sudo python mount.py

我更改的脚本

import os
import subprocess

if os.path.ismount("/media/New Volume"):
    subprocess.Popen(["nautilus", "/media/New Volume"])
else:
    path = "/media/New Volume"
    os.mkdir(path);
    subprocess.Popen(["mount","-t","fuseblk","/dev/sda4","/media/New Volume"])
    subprocess.Popen(["nautilus", "/media/New Volume"])

【问题讨论】:

  • 刚刚检查,您确定目录/mnt/New Volume 已创建,对吗?请记住,mount 不会自行创建它。
  • mount 命令看起来并不好。语法应该是mount -t <file system type> <device> <directory>
  • 你通常还需要root权限才能挂载
  • 最好使用subprocess.check_call函数,而不是直接构造subprocess.Popen对象。 check_call 函数将在非零退出代码上引发异常,当您与这样的环境交互时,您将希望处理这些异常情况。它还将等待被调用的进程终止,这是您绝对想要的,因为挂载文件系统可能需要一些时间(以避免 Nautilus 先打开并失败)。
  • 老问题,但对于未来的冒险。看看mount.py

标签: python linux ubuntu mount


【解决方案1】:

如果您的卷确实被称为New Volume,那么您可能需要考虑将其作为New\ Volume 输入到您的脚本中以避开单词之间的空格。否则,linux shell 不会将其识别为连接路径,并尝试将Volume 附加到之前发出的命令中...

【讨论】:

  • 默认情况下(在这种情况下),shell 不会处理参数列表,因此任何此类转义都是多余的。如果指定了可选参数shell=True,并且参数是字符串,那么这将是正确的。
  • 感谢您的澄清。我只是习惯直接在shell中挂载东西,并认为call将命令传递给shell...
猜你喜欢
  • 1970-01-01
  • 2011-01-10
  • 2019-03-13
  • 2011-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-06
  • 1970-01-01
相关资源
最近更新 更多