0

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 wanted.

Just like if a user entered test.txt for file and to change its permission for reading, writing and executing for all including group, owner and user. I know that, I need to run the terminal/bash command from python. But Is there any way to run bash command from python? Is there any external library/module in python or it comes built in?

Thanks in advance.

AmirSina Mashayekh
  • 498
  • 1
  • 5
  • 21
  • Better than running external programs: https://stackoverflow.com/questions/16249440/changing-file-permission-in-python – Klaus D. Jun 13 '20 at 04:20

1 Answers1

2

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

Kodi Learn
  • 270
  • 1
  • 3
  • 14