How to mount a network directory using python?

前端 未结 4 1863
闹比i
闹比i 2021-02-08 19:47

I need to mount a directory \"dir\" on a network machine \"data\" using python on a linux machine

I know that I can send the command via command line:

mk         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-08 20:38

    Example using the subprocess module:

    import subprocess
    
    subprocess.Popen(["mkdir", "~/mnt/data_dir", "mount", "-t", "data:/dir/", "/mnt/data_dir"])
    

    OR

    import subprocess
    
    subprocess.Popen("mkdir ~/mnt/data_dir mount -t data:/dir/ /mnt/data_dir", shell=True)
    

    The second version uses the shell to execute the command. While more readable and easier to use in most situations, it should be avoided when passing user submitted arguments as those might lead to shell injection (i.e. execution of other commands than mkdir in this case).

提交回复
热议问题