webminal.org
webminal.org

Reputation: 47196

how to invoke sshfs within python script?

I want to mount a remote directory using sshfs. sshfs working fine from terminal. But how to invoke it from within python script?

I tried something like this - but didn't work at all.

import os

cmd = "/usr/bin/sshfs [email protected]:/home/giis /mnt" 
os.system(cmd)

Upvotes: 2

Views: 2780

Answers (2)

Jerin Antony
Jerin Antony

Reputation: 383

import subprocess
mount_command = f'sshfs {host_username}@{host_ip}:{host_data_directory} {local_data_directory}'
subprocess.call(mount_command, shell=True)
# Do your stuff with mounted folder
unmount_command = f'fusermount -u  {local_data_directory}'
subprocess.call(unmount_command, shell=True)

Upvotes: 0

ghostdog74
ghostdog74

Reputation: 342263

first, you should make sure your sshfs command works fine using the shell. Then, go to here to see many examples of using subprocess module of Python to call your sshfs commmand

Upvotes: 3

Related Questions