如何自动授予文件权限?

Zhang Yifan

当我尝试运行python文件时,说hello.py,提示

bash: ./hello.py: Permission denied

所以我需要

chmod u+x hello.py

在我运行它之前。

创建所有python文件后,是否可以自动授予对它们的访问权限?

苏拉夫角

您可以使用像这样的简单脚本,

#!/bin/bash

if [ $# -lt 2 ]
then
    chmod +x $(pwd)/$1
    $(pwd)/$1
else
    chmod +x $(pwd)/$1
    $(pwd)/$1 $2
fi

将上述脚本另存为runpy.sh,保存在其中PATH(可以保存在中~/bin

从终端授予执行权限,

chmod +x ~/bin/runpy.sh

用法

  • 要在hello.py不更改其权限的情况下在终端中运行,
runpy.sh hello.py
  • 如果您想在python程序的参数中使用任何内容,请在参数中输入" "like,
runpy.sh hello.py "-option arg1 arg2 agr3"

它应该可以解决问题。但是不要忘了在python(.py)文件中使用shebang行

#!/usr/bin/python

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章