How do I mount a filesystem using Python?

前端 未结 10 1951
鱼传尺愫
鱼传尺愫 2020-12-16 09:41

I\'m sure this is a easy question, my Google-fu is obviously failing me.

How do I mount a filesystem using Python, the equivalent of running the shell command

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 10:09

    As others have stated, a direct access to the syscall will not help you, unless you're running as root (which is generally bad, for many reasons). Thus, it is best to call out to the "mount" program, and hope that /etc/fstab has enabled mounts for users.

    The best way to invoke mount is with the following:

    subprocess.check_call(["mount", what])
    

    where what is either the device path, or the mountpoint path. If any problems arise, then an exception will be raised.

    (check_call is an easier interface than Popen and its low-level brethren)

提交回复
热议问题