Skip to content Skip to sidebar Skip to footer

How To Run Bash Command From Using Python

I want to create a program that can change the file permission of a file in linux. In my program it will ask for the file location and change the permission of file that the user w

Solution 1:

Well, you do not need to download any python library for it. It comes builtin. You just need to is import one module named subprocess like this:

import subprocess

After importing you can run the bash command like this: If you want to see the list of files then just type like this:

subprocess.run([“ls”])

You need to run in list because if there is some parameter then you need to give it inside the list. Just like this: If you want to install one app through bash then type this command:

subprocess.run([“sudo”, “apt”, “install”, “vlc”])

But remember not to put space in bash command otherwise it will give you an error. And if there are more parameters then add or append it to the list. Just like I done above.

Learn more about it here

Post a Comment for "How To Run Bash Command From Using Python"