如何授予Linux中所有Python脚本可执行权限?
假设我有一个名为 的python脚本" title="python脚本">python脚本,如下所示:
#!/usr/bin/env python3# -*- coding: utf-8 -*-
# Author : Bhishan Poudel
# Date : Jul 13, 2016
# Imports
# Script
print("hello")
我可以通过两种方式运行此脚本:
python3 a.py
chmod a+x a.py; ./a.py
如何在不使用chmod a+x script_name
所有时间的情况下运行任何新的或旧的python脚本。
我对我的计算机具有root访问权限和用户访问权限。
基本上我想要所有.py文件的可执行权限,我们该怎么做?
我尝试了不同的shebang,例如:
#!/usr/bin/python3#!/usr/bin/env python3
#!/usr/local/bin/python3
#!/usr/local/bin/env python3
python解释器也位于$ PATH中。echo $ PATH的输出如下:
/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/opt/local/bin:/Users/poudel/phosim:/Users/poudel/Applications:/usr/local/octave/3.8.0/bin:/Users/poudel/Applications/Geany.app/Contents/MacOS/:/opt/local/bin:/Users/poudel/phosim:/Users/poudel/Applications:/usr/local/octave/3.8.0/bin:/Applications/Geany.app/Contents/MacOS/:/opt/local/bin:/Users/poudel/phosim:/Users/poudel/Applications:/usr/local/octave/3.8.0/bin:/Applications/Geany.app/Contents/MacOS/
此外,ls / usr / bin / py *具有:
/usr/bin/pydoc* /usr/bin/python2.5@ /usr/bin/pythonw*/usr/bin/pydoc2.5@ /usr/bin/python2.5-config@ /usr/bin/pythonw2.5@
/usr/bin/pydoc2.6@ /usr/bin/python2.6@ /usr/bin/pythonw2.6@
/usr/bin/pydoc2.7@ /usr/bin/python2.6-config@ /usr/bin/pythonw2.7@
/usr/bin/python* /usr/bin/python2.7@
/usr/bin/python-config* /usr/bin/python2.7-config@
回答:
以root权限运行以下命令:
find /your/path/ -type f -name "*.py" -exec chmod u+x {} \;
chmod
如果您是.py
文件的所有者,则无需以root用户身份运行。
编写脚本来解决这个问题。
#!/bin/bashif [ -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
将脚本另存为,pycreator
例如/usr/bin/
,然后说
chown root:root /usr/bin/pycreatorchmod +x-w /usr/bin/pycreator
要使用创建新脚本pycreator
,请执行
pycreator calculator.py
以上是 如何授予Linux中所有Python脚本可执行权限? 的全部内容, 来源链接: utcz.com/qa/414821.html