如何使Python脚本可执行 chmod 755?

4 投票
5 回答
4239 浏览
提问于 2025-04-15 11:49

我的主机服务商说我的Python脚本必须设置为可执行的(chmod 755)。这是什么意思,我该怎么做呢?

谢谢!

5 个回答

1

这句话的意思是,有人(用户、一个小组或者所有人)有权利去执行(或者读取、写入)这个脚本(或者一般的文件)。

权限的表示方式有很多种:

$ chmod +x file.py # makes it executable by anyone
$ chmod +w file.py # makes it writeabel by anyone
$ chmod +r file.py # makes it readably by anyone

$ chmod u+x file.py # makes it executable for the owner (user) of the file
$ chmod g+x file.py # makes it executable for the group (of the file)
$ chmod o+x file.py # makes it executable for the others (everybody)

你可以用同样的方法来取消权限,只需要把+换成-就可以了。

$ chmod o-x file.py # makes a file non-executable for the others (everybody)
$ ...

八进制数字用另一种方式来表示权限。4代表读取,2代表写入,1代表执行。

简单的数学:

read + execute = 5
read + write + execute = 7
execute + write = 3
...

把这些都打包成一个简短而有效的命令:

# 1st digit: user permissions
# 2nd digit: group permissions
# 3rd digit: 'other' permissions

# add the owner all perms., 
# the group and other only write and execution

$ chmod 755 file.py
5

类Unix系统有一种叫“文件模式”的东西,它用来说明谁可以读取、写入或执行一个文件。模式755的意思是文件的拥有者可以读取、写入和执行,而其他人只能读取和执行,不能写入。要让你的Python脚本具有这个模式,你需要输入

chmod 0755 script.py

另外,你还需要在文件的第一行加一个叫“shebang”的东西,比如

#!/usr/bin/python

这样可以告诉操作系统这个脚本是什么类型的。

5

如果你可以通过ssh访问你的网页空间,连接后输入以下命令:

chmod 755 nameofyourscript.py

如果你没有ssh访问权限,而是通过FTP连接,那么你需要查看你的FTP软件,看看它是否支持设置权限。

关于755的意思:

  • 第一个数字是用户设置(就是你自己)
  • 第二个数字是组设置
  • 第三个数字是系统其他用户的设置

这些数字是通过加权限值来构成的。1代表可执行,2代表可写,4代表可读。也就是说,755的意思是你自己可以读、写和执行这个文件,而其他人只能读和执行它。

撰写回答