案例解析
這個問題描述起來有點違反直覺,要執行一個檔案不應該需要可執行權限嗎?讓我們先來看一個例子:
# module1.py def test(): print ('hello world!') if __name__ == '__main__': test()
這是一個名為module1.py的文件,這個文件只有可讀權限:
[dechin@dechin-manjaro excute ]$ ll
-r--r--r-- 1 dechin dechin 78 1月15 17:06 module1.py
我們可以直接用python來執行這個檔案:
[dechin@dechin-manjaro excute]$ python3 module1.py
hello world!
我們發現即使只有可讀權限,這個檔案也是可以運作的。為了嚴格驗證,我們在這裡創建另外一種模式的測試,透過import來導入python文件,是否也不需要可執行權限呢?
# module2.py from module1 import test if __name__ == '__main__': test()
相同的,我們新建的檔案也未賦予可執行權限:
#[dechin@dechin-manjaro excute]$ ll
-r--r-- r-- 1 dechin dechin 78 1月15 17:06 module1.py
-r--r--r-- 1 dechin dechin 64 1月15 17:44 module2.py
#。 #我們執行一下module2.py這個檔案:
[dechin@dechin-manjaro excute]$ python3 module2.py那麼我們的測試就完成了,經過驗證,執行普通的py檔案是不需要可執行權限的,這對我們的權限最小化約束就產生了一定的啟發作用。 原理解釋在stackoverrun上面有一條回复,作者cedbeu是這樣描述的:python本身承擔了語言解析器的角色,py文件不過是一個文本文件,真正執行的二進制檔案是python而不是使用者所建立的py檔案。因此,即使去掉py檔案的可執行權限,該py檔案也是可以透過python來執行的。但是,如果我們去掉了python的可執行權限,那就無法正常執行這條任務了。 擴充測試hello world!
├── module1 .py###├── module2.py###└── __pycache__### └── module1.cpython-38.pyc### ###1 directory, 3 files###[dechin@dechin- manjaro excute]$ cd __pycache__/###[dechin@dechin-manjaro __pycache__]$ ll###總用量4###-rw-r--r-- 1 dechin dechin 259 1月15 18:01 module1. cpython-38.pyc#########這裡我們看到pyc檔的檔名會固定有個後綴,同樣也沒有可執行權限,這裡我們用同樣的指令來執行pyc檔:### ######[dechin@dechin-manjaro __pycache__]$ ll###-r--r--r-- 1 dechin dechin 259 1月15 18:01 module1.cpython-38.pyc###- rw-r--r-- 1 dechin dechin 259 1月15 18:13 module1.pyc###-r--r--r-- 1 dechin dechin 64 1月15 18:09 module2.py### [dechin@dechin-manjaro __pycache__]$ python3 module1.cpython-38.pyc ###hello world!###[dechin@dechin-manjaro __pycache__]$ python3 module2.py ###hello world!##### ####這裡我們可以發現,不論是直接執行pyc文件,或是改名為module1.pyc之後再透過module2.py導入的方式,都可以正常的被執行,而且都不具有可執行權限。接下來我們再來試試pyo檔:#########[dechin@dechin-manjaro excute]$ python3 -O -m py_compile module1.py #########執行有opt的pyc檔:#########[dechin@dechin-manjaro __pycache__]$ python3 module1.cpython-38.opt-1.pyc ###hello world!#########相同的,都可以正常的被執行,即使沒有可執行權限。 ######技術彩蛋######即使我們把pyc檔案強行改名為py文件,同樣也是不影響任務執行的:#########[dechin@dechin-manjaro __pycache__] $ cp module1.cpython-38.opt-1.pyc module1.py###[dechin@dechin-manjaro __pycache__]$ ll###總用量20###-rw-r--r-- 1 dechin dechin 259 1月15 18:17 module1.cpython-38.opt-1.pyc###-r--r--r-- 1 dechin dechin 259 1月15 18:01 module1.cpython-38.pyc### #-rw-r--r-- 1 dechin dechin 259 1月15 18:20 module1.py###-rw-r--r-- 1 dechin dechin 259 1月15 18:13 module1.pyc## #-r--r--r-- 1 dechin dechin 64 1月15 18:09 module2.py###[dechin@dechin-manjaro __pycache__]$ python3 module1.py###hello world!#### ##如果將py檔案編譯成pyc和pyo格式的文件,此時的任務執行是否需要可執行權限呢?首先測試pyc檔案:
.
[dechin@dechin-manjaro excute]$ python3 -m py_compile module1.py
執行完編譯,我們會在目前目錄下發現一個__pycache__的資料夾,編譯好的pyc檔就儲存在這個目錄下:
[dechin@dechin-manjaro excute]$ tree
以上是執行Python腳本時是否需要新增可執行權限?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Tomergelistsinpython,YouCanusethe操作員,estextMethod,ListComprehension,Oritertools

在Python3中,可以通過多種方法連接兩個列表:1)使用 運算符,適用於小列表,但對大列表效率低;2)使用extend方法,適用於大列表,內存效率高,但會修改原列表;3)使用*運算符,適用於合併多個列表,不修改原列表;4)使用itertools.chain,適用於大數據集,內存效率高。

使用join()方法是Python中從列表連接字符串最有效的方法。 1)使用join()方法高效且易讀。 2)循環使用 運算符對大列表效率低。 3)列表推導式與join()結合適用於需要轉換的場景。 4)reduce()方法適用於其他類型歸約,但對字符串連接效率低。完整句子結束。

pythonexecutionistheprocessoftransformingpypythoncodeintoExecutablestructions.1)InternterPreterReadSthecode,ConvertingTingitIntObyTecode,whepythonvirtualmachine(pvm)theglobalinterpreterpreterpreterpreterlock(gil)the thepythonvirtualmachine(pvm)

Python的關鍵特性包括:1.語法簡潔易懂,適合初學者;2.動態類型系統,提高開發速度;3.豐富的標準庫,支持多種任務;4.強大的社區和生態系統,提供廣泛支持;5.解釋性,適合腳本和快速原型開發;6.多範式支持,適用於各種編程風格。

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中