Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. วิธีสร้างชื่อไดเรกทอรีที่ไม่ซ้ำกันด้วย Python

    ใช้โมดูล tempfile สร้าง temporary directory แบบปลอดภที่สุด ไม่มี race condition ไดเรกทอรีอ่าน/เขียน/ค้นหาได้เฉพาะ user ที่สร้าง import tempfile _, temp_dir_path = tempfile.mkdtemp() # ทำงานกับไดเรกทอรีนี้ # ลบเองเมื่อใช้เสร็จ หมายเหตุ: ต้องลบไดเรกทอรีนี้เองหลังใช้งานเสร็จ

  2. วิธีลบไฟล์ด้วย Python

    ใช้ฟังก์ชันในโมดูล os ลบไฟล์เดี่ยวหรือโฟลเดอร์ว่างเดี่ยว ตัวอย่าง: ลบไฟล์ a.txt >>> import os >>> os.remove(a.txt) อาร์กิวเมนต์ต้องเป็น absolute หรือ relative path ใช้ os.unlink() ได้เช่นกัน (ทำงานเหมือนกัน): >>> import os >>> os.unlink(a.txt)

  3. วิธีแทนที่ตัวเลขด้วยคำอ่านใน Python

    ใช้ dictionary mapping ตัวเลขเป็นคำอ่าน: dct = {0: zero, 1: one, 2: two, 3: three, 4: four, 5: five, 6: six, 7: seven, 8: eight, 9: nine} วนลูปแต่ละตัวอักษร ตรวจสอบด้วย isdigit() ถ้าเป็นตัวเลข ใช้ dictionary mapping แทนที่: string = I have 3 Networking books, 0 Database books, and 8 Programmi

  4. วิธีตั้งค่าวันที่สร้างและแก้ไขไฟล์ด้วย Python

    ใน Windows ใช้ os.path.getctime(file_path) สำหรับ creation time บน UNIX ใช้ st_birthtime จาก stat tuple (ไม่รองรับ Linux ทุกตัว) ตัวอย่าง Windows >>> import os >>> print(os.path.getctime(my_file)) 1505928271.0689342 ค่าเป็นวินาทีตั้งแต่ epoch สำหรับ UNIX: import os stat = os.stat(p

  5. วิธีเขียน Binary Data ลงไฟล์ด้วย Python

    Binary files คือไฟล์ที่รูปแบบไม่ได้ประกอบด้วยอักขระอ่านได้ เช่น รูปภาพ JPEG/GIF, เสียง MP3, หรือเอกสาร Word/PDF ใน Python ไฟล์เปิดใน text mode โดย default ต้องเพิ่ม b ใน mode เพื่อเปิดใน binary mode ตัวอย่าง f = open(my_file, w+b) byte_arr = [120, 3, 255, 0, 100] binary_format = bytearray(byte_arr)

  6. วิธี Import ไฟล์ Python อื่นๆ

    ต้อง import ก่อนจึงจะใช้แพ็กเกจ/โมดูลได้ ใน Python บางอย่างเป็น built-in (เช่น int, float) ใช้ได้ทันที แต่อื่นๆ ต้อง import ก่อน ตัวอย่าง >>> math.cos(0) Traceback (most recent call last): File , line 1, in NameError: name math is not defined >>> import math >>> math.co

  7. ไฟล์ .pyc ใน Python คืออะไร?

    ไฟล์ .pyc ถูกสร้างโดย Python interpreter เมื่อมีการ import ไฟล์ .py ไฟล์นี้เก็บ compiled bytecode ของโมดูล/โปรแกรมที่ import เพื่อให้ข้ามขั้นตอนแปลง source code เป็น bytecode (ซึ่งต้องทำเพียงครั้งเดียว) ได้ในครั้งถัดไป หาก .pyc ใหม่กว่า .py จะช่วยให้ startup เร็วขึ้นเล็กน้อย แต่ยังคงเป็น interpreted

  8. ความหมายของ副檔名 Python: .pyc .pyd .pyo

    ไฟล์แต่ละประเภทมีความหมายเฉพาะ: .py - ไฟล์ source code ที่คุณเขียน .pyc - Compiled bytecode เมื่อ import โมดูล Python จะสร้างไฟล์นี้เพื่อทำให้ import ครั้งต่อไปเร็วขึ้น .pyo - ไฟล์ .pyc ที่สร้างขณะเปิด optimization (-O flag) .pyd - Windows DLL file สำหรับ Python (รายละเอียด)

  9. รูปแบบ Header มาตรฐานของไฟล์ Python

    นักพัฒนา Python มักจัดระเบียบโมดูล/ไฟล์ดังนี้: บรรทัดแรก: #!/usr/bin/env python - ทำให้รันไฟล์เป็นสคริปต์ได้โดยไม่ต้องพิมพ์ python นำหน้า ต่อมา: Docstring ที่อธิบายไฟล์/โมดูล จากนั้น: โค้ดทั้งหมด รวม import statements - import built-in modules ก่อน ตามด้วย third-party แล้วค่อย path modifications แล

  10. วิธี List ไฟล์ทั้งหมดในไดเรกทอรีด้วย Python

    os.listdir(my_path) จะคืนรายการทั้งหมดในไดเรกทอรี (ไฟล์และโฟลเดอร์) ตัวอย่าง >>> import os >>> os.listdir(.) [DLLs, Doc, etc, include, Lib, libs, LICENSE.txt, NEWS.txt, python.exe, pythonw.exe, README.txt, Scripts, share, tcl, Tools, w9xpopen.exe] กรองเฉพาะไฟล์ >>> import

  11. วิธีหาไฟล์ .txt ทั้งหมดในไดเรกทอรีด้วย Python

    ใช้ os.listdir ดึงรายการทั้งหมด แล้วกรองเฉพาะไฟล์ที่ลงท้ายด้วย .txt ตัวอย่าง >>> import os >>> file_list = [f for f in os.listdir(.) if os.path.isfile(os.path.join(., f)) and f.endswith(.txt)] >>> print(file_list) [LICENSE.txt, NEWS.txt, README.txt] เมธอด endswith ตรวจสอบว

  12. วิธี Copy ไฟล์ Binary ใน Python

    โมดูล shutil มีฟังก์ชันสำหรับ copy ไฟล์และโฟลเดอร์ทั้งหมด shutil.copy(source, destination) จะ copy ไฟล์จาก source ไปยังโฟลเดอร์ destination หาก destination เป็นชื่อไฟล์ จะใช้เป็นชื่อไฟล์ใหม่ ฟังก์ชันคืนค่าพาธของไฟล์ที่ copy แล้ว ตัวอย่าง >>> import shutil >>> shutil.copy(original.t

  13. วิธีให้ไฟล์ Python หนึ่งรันอีกไฟล์ Python หนึ่ง

    มีหลายวิธีให้ไฟล์ Python รันอีกไฟล์: 1. ใช้เป็นโมดูล (แนะนำ) import แล้วเรียกฟังก์ชัน: import fileB fileB.my_func() 2. ใช้ exec (Python 2) execfile(file.py) 3. สร้าง process ใหม่ด้วย os.system ตัวอย่าง os.system(python my_file.py) หมายเหตุ: วิธีที่ 1 สะอาดและเป็นมาตรฐานที่สุด วิธีที่ 2 ล้าสแล้วใน Py

  14. การทำงานของ Underscore (_) ใน Python

    Underscore (_) มีความหมายพิเศษใน Python 5 กรณีหลัก: 1. เก็บค่า expression ล่าสุดใน interpreter >>> 12 + 10 22 >>> _ 22 2. เพื่อ ignore ค่าที่ไม่ต้องการ >>> _, _, a = (1, 2, 3) >>> a 3 3. ให้ความหมายพิเศษต่อชื่อตัวแปร/ฟังก์ชัน Single leading underscore (_var) ใช้สำ

  15. วิธี Source ไฟล์ Python จากอีกไฟล์ Python หนึ่ง

    ต้องใช้เหมือนโมดูล คือ import ไฟล์ที่ต้องการรัน แล้วเรียกฟังก์ชันภายใน เช่น ต้องการ import fileB.py เข้า fileA.py (อยู่โฟลเดอร์เดียวกัน) ใน fileA เขียน: import fileB ใน fileA เรียกฟังก์ชันใน fileB ได้: fileB.my_func() ใน fileB ต้องมีฟังก์ชัน my_func ก่อน: ตัวอย่าง def my_func(): print(Hello from

  16. วิธีแยกไฟล์ Binary หลายไฟล์ด้วย Python

    เพื่อแยกไฟล์ binary ใหญ่เป็นชิ้นเล็กๆ ให้อ่านตามขนาด chunk ที่ต้องการ เขียนลงไฟล์ใหม่ ทำซ้ำจนจบไฟล์ต้นฉบับ ตัวอย่าง แยก my_song.mp3 เป็นไฟล์ละ 500 bytes: CHUNK_SIZE = 500 file_number = 1 with open(my_song.mp3, rb) as f: chunk = f.read(CHUNK_SIZE) while chunk: with open(my_song_part_

  17. วิธี Import Python Module จาก Full Path

    วิธีง่ายที่สุดคือเพิ่มพาธเข้าไปใน sys.path ซึ่งเป็นรายการไดเรกทอรีที่ Python interpreter ค้นหาโมดูล ตัวอย่าง import sys sys.path.append(/foo/bar/my_module) # สมมติโมดูลมีฟังก์ชัน my_func from my_module import my_func # หรือ import โมดูลทั้งหมด import my_module

  18. วิธีอ่านไฟล์จาก Command Line ด้วย Python

    สคริปต์ต้องรับ CLI argument เพื่ออ่านไฟล์ ตัวอย่างโปรแกรม cat แบบ Python: import sys with open(sys.argv[1], r) as f: contents = f.read() print(contents) บันทึกเป็น cat.py รันด้วย: $ python cat.py my_file.txt จะอ่าน my_file.txt และแสดงเนื้อหาออกหน้าจอ sys.argv[1] คือ argument ที่สอง

  19. วิธีเขียนไฟล์จาก Command Line ด้วย Python

    สคริปต์ต้องรับ CLI argument เพื่อเขียนไฟล์จาก command line ตัวอย่าง: โปรแกรมต่อท้าย Hello ลงไฟล์ import sys with open(sys.argv[1], a) as f: f.write(Hello) ผลลัพธ์ บันทึกเป็น cat.py แล้วรัน: $ python cat.py my_file.txt เปิด my_file.txt จะพบว่า Hello ถูกเพิ่มท้ายไฟล์ sys.argv[1] คือ argument ที่ 2

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:25/451  20-คอมพิวเตอร์/Page Goto:1 19 20 21 22 23 24 25 26 27 28 29 30 31