การรันสคริปต์ Python บน command line
มาลองดูตัวอย่างการใช้งาน Python บน command line โดยไม่ต้องเปิด GUI อย่าง Jupyter Notebook หรือ Spyder การโต้ตอบกับ Python ผ่าน command line นั้นรวดเร็วและมีประสิทธิภาพกว่าการใช้ GUI ในแบบฝึกหัดนี้ เราจะสร้างไฟล์ Python และรันไฟล์นั้นด้วย Python โดยตรง โดยไม่ต้องออกจาก bash terminal เลย
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การประมวลผลข้อมูลบน Command Line
คำแนะนำการฝึกหัด
- สร้างไฟล์ Python ใหม่และส่งคำสั่ง print ของ Python เข้าไปในไฟล์นั้นในขั้นตอนเดียว
- รันไฟล์ Python ที่สร้างขึ้นโดยเรียกใช้โดยตรงจาก command line
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# 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.___