在命令行执行 Python 脚本
让我们通过一个示例来演示如何在命令行使用 Python,而无需打开 Jupyter Notebook 或 Spyder 等图形界面。相比 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.___