การประมวลผลข้อมูลบน Command Line
Susan Sun
Data Person
Python
เอกสารประกอบ:
man python
...
-V , --version
Prints the Python version number of the executable and exits.
python --version
Python 3.5.2
ตัวอย่าง: ใช้ Python แบบ native
which python
/usr/bin/python
เปิด Python interactive session ใน terminal:
python
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linuxType "help", "copyright", "credits" or
"license" for more information.
>>>
ภายใน interactive session ใช้ได้เฉพาะ Python syntax:
>>> print('hello world')
hello world
ออกจาก Python session และกลับสู่ terminal:
>>> exit()
$
Python interactive session:
ทางเลือก:
.pypython + ชื่อสคริปต์วิธีที่ 1
.py ด้วย text editor บนคอมมานด์ไลน์ เช่น nano, Vim, Emacs
วิธีที่ 2
.py โดยใช้ echo เขียน Python syntax ลงในไฟล์ hello_world.py ในขั้นตอนเดียวecho "print('hello world')" > hello_world.py
ตรวจสอบเนื้อหาในไฟล์:
cat hello_world.py
print('hello world')
ตรวจสอบว่าอยู่ใน directory เดียวกับไฟล์ .py:
ls
hello_world.py
รันไฟล์ .py โดยใส่ python นำหน้าชื่อไฟล์:
python hello_world.py
hello world
การประมวลผลข้อมูลบน Command Line