如何使用sshfs和Python Subprocess安装和卸载文件夹?

时间:2016-12-24 20:28:27

标签: python subprocess sshfs

我希望能够从Python子进程模块安装然后卸载调用sshfs的目录。这是我用来完成此任务的代码。

import subprocess 
mkdir_command = 'mkdir {}'.format(local_data_directory)
unmount_command = 'umount {}'.format(local_data_directory)
mount_command = 'sshfs -o allow_other -o IdentityFile={} {}@{}:{} {}'.format(
    key_file, host_username, host_ip, host_data_directory, local_data_directory)
subprocess.call(mkdir_command, shell=True)
subprocess.call(mount_command, shell=True)
subprocess.call(unmount_command, shell=True)

mkdir和mount命令成功,但是当我尝试卸载目录时,我收到错误umount failed:操作不被允许。我猜测这是因为子进程用户对local_data_directory的父文件夹没有写入权限。当我检查local_data_directory的权限时,它说所有者是用户#1004。这是Python的子进程的默认用户吗?我想我可以让该用户对所有父目录进行写访问,但我不想为我的整个主文件夹提供子进程写入功能。有没有办法解决这个问题而不这样做?

1 个答案:

答案 0 :(得分:1)

原来解决方案是使用fusermount而不是mount

import subprocess 
mkdir_command = 'mkdir {}'.format(local_data_directory)
unmount_command = 'fuserumount {}'.format(local_data_directory)
mount_command = 'sshfs -o allow_other -o IdentityFile={} {}@{}:{} {}'.format(
    key_file, host_username, host_ip, host_data_directory, local_data_directory)
subprocess.call(mkdir_command, shell=True)
subprocess.call(mount_command, shell=True)
subprocess.call(unmount_command, shell=True)