how to invoke sshfs within python script?

Question:

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)
Asked By: webminal.org

||

Answers:

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

Answered By: ghostdog74
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)
Answered By: Jerin Antony
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.