在命令列執行 Python 指令稿
來看看如何在命令列使用 Python,而不需要打開像 Jupyter Notebook 或 Spyder 這樣的 GUI。相較於 GUI,在命令列與 Python 互動通常更快也更有效率。這裡你會建立一個 Python 檔案,並用系統內建的 Python 來執行,全程都留在 bash 終端機內完成。
本練習屬於課程
在 Shell 中進行資料處理
練習說明
- 用一個步驟建立新的 Python 檔案,並把 Python 的列印指令寫入該檔案。
- 直接從命令列呼叫並執行這個新的 Python 檔案。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# in one step, create a new file and pass the print function into the file
echo "print('This is my first Python script')" ___ my_first_python_script.py
# check file location
ls
# check file content
cat my_first_python_script.py
# execute Python script file directly from command line
___ my_first_python_script.___