How to give executable permission to all Python scripts in Linux?

后端 未结 4 872
说谎
说谎 2020-12-21 04:41

Suppose I have a python script called a.py like this:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author    : Bhishan Poudel
# Date             


        
相关标签:
4条回答
  • 2020-12-21 05:14

    Using the idea of @sjsam, I did following:

    Suppose I have a file hello.py in any location.

    cd to that location
    find $PWD -type f -name "*.py" -exec chmod u+x {} \;
    ./hello.py
    
    # Now, i can create any number of .py files in that folder and run ./filename
    
    # Note: if we are running as user permission, and also have sudo access,
       we can also do:
       sudo -H find $PWD -type f -name "*.py" -exec chmod u+x {} \;
    
    We should not use sudo unless absolutely necessary.
    

    Thanks to sjsam.

    0 讨论(0)
  • 2020-12-21 05:18

    The hard way

    Run below with root privilege:

    find /your/path/ -type f -name "*.py" -exec chmod u+x {} \;
    

    Note:

    chmod need not be run as root if you're the owner of .py file.

    The smart way

    Write a script to take care of this.

    #!/bin/bash
    if [ -f "$1" ]
    then
    geany "$1" # You could also use xdg-open if you set geany to open .py files
    else
    cp /path/to/python/startup/template "$1" # You may omit this if you don't have a default template
    chmod u+x "$1"
    geany "$1"
    fi
    

    Save the script as, say, pycreator in say /usr/bin/ , then do

    chown root:root /usr/bin/pycreator
    chmod +x-w /usr/bin/pycreator
    

    To create a new script using pycreator, do

    pycreator calculator.py
    

    Also [ this ] answer pointed to by @choroba in his comment provides valuable insight in this regard.

    0 讨论(0)
  • 2020-12-21 05:27

    Just type this command in terminal.

    sudo su
    
    0 讨论(0)
  • 2020-12-21 05:30

    Try this chmod +x *.py it works on my PC(OS:Ubuntu 20.4), Also i am using #! /usr/bin/env python3 shebang

    0 讨论(0)
提交回复
热议问题