Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set execute bit for a file using python

Using python on Mac OS, I would like to open a file for writing and put some shell commands into it. Later to be run in terminal.

with open("my_script.sh", "w") as fd:
    fd.write("#!/bin/sh\n")
    fd.write("echo $PATH\n")

This will create the file, but I could not figure how to set the execute bit so when I run it in the Terminal I will not get:

sh: my_script.sh: Permission denied
like image 506
Periodic Maintenance Avatar asked Dec 31 '12 18:12

Periodic Maintenance


1 Answers

import os
os.chmod("my_script.sh", 0744)

Pick the value properly though. Some values might not be safe.

like image 61
Tom van der Woerdt Avatar answered Oct 17 '22 06:10

Tom van der Woerdt