Home > Back-end >  How can I change a file mod to chmod x in linux?
How can I change a file mod to chmod x in linux?

Time:11-29

I have a helloworld.sh file in my Linux. When I try to simply run it through a python script with subprocess.call('./hello.sh'), it shows the output of permission denied.

I have tried this method found online but it is not working os.chmod("hello.sh", 0o664). I want to change the permission of the file through a python script to chmod x.

Please guide me through the syntax. I also have searched online but it is not working. I have a Debian-based Linux.

CodePudding user response:

This should add executable permissions to user, group and other in a platform independent way while maintaining any original permissions. I don't own a Windows machine to test on, but the documentation of Path.chmod() says that it should be supported on Windows as well, although all bits except the read-only bit will be ignored.

from pathlib import Path
import stat

path = Path("/path/to/file")
original_st_mode = path.st_mode
path.chmod(original_st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)

CodePudding user response:

Could you try os.popen('chmod x hello.sh') prior to calling subprocess.call()?

  • Related