Why use Python's os module methods instead of executing shell commands directly?

前端 未结 6 1073
耶瑟儿~
耶瑟儿~ 2021-01-29 17:58

I am trying to understand what is the motivation behind using Python\'s library functions for executing OS-specific tasks such as creating files/directories, changing file attri

6条回答
  •  天命终不由人
    2021-01-29 18:42

    It is safer. To give you an idea here is an example script

    import os
    file = raw_input("Please enter a file: ")
    os.system("chmod 777 " + file)
    

    If the input from the user was test; rm -rf ~ this would then delete the home directory.

    This is why it is safer to use the built in function.

    Hence why you should use subprocess instead of system too.

提交回复
热议问题