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)
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)
chmod +x test.py
?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只需要读取权限才能执行我的文件?
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 只需要读取文件内容。回想一下,Python 是一种解释型语言(如 PHP、Ruby 等),它只是处理该文件的内容,而不是执行它;python
是这里的可执行文件!
获取适当的背景信息;请注意,您可以通过两种方式运行脚本:
使用文件作为输入/参数调用解释器不需要读取权限,例如:
python myscript.py
Run Code Online (Sandbox Code Playgroud)通过shebang 运行脚本确实需要设置可执行位。
./myscript.py
Run Code Online (Sandbox Code Playgroud)
shebang(文件中的第一行)就像
#!/usr/bin/env python
Run Code Online (Sandbox Code Playgroud) 归档时间: |
|
查看次数: |
4633 次 |
最近记录: |