Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何装载和卸载带有sshfs和Python子进程的文件夹?_Python_Subprocess_Sshfs - Fatal编程技术网

如何装载和卸载带有sshfs和Python子进程的文件夹?

如何装载和卸载带有sshfs和Python子进程的文件夹?,python,subprocess,sshfs,Python,Subprocess,Sshfs,我希望能够从pythonsubaccess模块装载并卸载一个调用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={} {}@

我希望能够从pythonsubaccess模块装载并卸载一个调用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:不允许操作。我猜这是因为子流程用户对本地\u data\u目录的父文件夹没有写权限。当我检查本地数据目录的权限时,它说所有者是用户1004。这是Python子流程的默认用户吗?我想我可以给那个用户所有父目录的写权限,但我不想给我整个主文件夹的子进程写权限。有没有办法解决这个问题而不这样做?

原来解决办法是使用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)

您能否在命令行上挂载目录(即成功执行完全相同的命令)?在类似debian的系统上,您需要属于
fuse
组才能这样做。@hiro Progator不,我也不能这样做。那么这就不是python的问题了。在转向python之前,您需要先运行它。google
sshfs fuse
与linux发行版的名称和版本结合使用。顺便说一下:python的子流程没有默认用户。该进程将由运行运行您的程序的python解释器的用户拥有。通常是您的用户id。在这种情况下,“密钥文件”是什么?如果在
/etc/fstab
umount
中输入正确,则“密钥文件”也会起作用……在这种情况下是什么?