即使python文件的权限是644,它为什么是可执行的?

don*_*oey 5 permissions python

我想我在这里误解了一些东西。我制作了一个简单的 python 测试文件来查看权限如何影响 python 文件的使用。我这样做是为了能够回答64bit ubuntu 12.04 python cannot run an existing python file

设置

我已经制作了一个包含内容的 test.py 文件

print 'I am working'
Run Code Online (Sandbox Code Playgroud)

测试用例 1

ls -al test.py 
-rw-r--r-- 1 joey joey 25 Dec 24 11:11 test.py
python test.py
I am working
Run Code Online (Sandbox Code Playgroud)
  • 即使我没有这样做,python 怎么会执行这个文件chmod +x test.py

测试用例 2

chmod 400 test.py
ls -al test.py 
-r-------- 1 joey joey 25 Dec 24 11:11 test.py
python test.py
I am working
Run Code Online (Sandbox Code Playgroud)

所以显然python只需要读取权限才能执行我的文件?

测试案例 3

chmod 200 test.py
ls -al test.py 
--w------- 1 joey joey 25 Dec 24 11:11 test.py
python test.py
python: can't open file 'testo.py': [Errno 13] Permission denied
Run Code Online (Sandbox Code Playgroud)

写权限是不够的(并且记录下来,只有可执行权限也是不够的)。

  • python是如何在没有可执行权限的情况下执行文件的?

ger*_*ijk 8

是的,Python 只需要读取文件内容。回想一下,Python 是一种解释型语言(如 PHP、Ruby 等),它只是处理该文件的内容,而不是执行它;python是这里的可执行文件!

获取适当的背景信息;请注意,您可以通过两种方式运行脚本: