Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. การแยกไฟล์ .txt จาก Zip File ใน Python (zipfile)

    ใช้โมดูล zipfile วนลูปไฟล์ใน Zip ตรวจสอบนามสกุล .txt แล้ว Extract ตัวอย่าง import zipfile my_zip = zipfile.ZipFile(my_zip_file.zip) # เปิดไฟล์ Zip storage_path = . # โฟลเดอร์ปลายทาง for file in my_zip.namelist(): if my_zip.getinfo(file).filename.endswith(.txt): my_zip.extract(file,

  2. ขีดจำกัดขนาดไฟล์สูงสุดที่ Python เปิดได้

    ไม่มีขีดจำกัดที่เข้าถึงได้ (No reachable maximum) สำหรับขนาดไฟล์ที่ Python เปิดได้ ผู้คนโหลดข้อมูลขนาด Gigabytes เข้า Memory ได้ปกติ ขีดจำกัดปฏิบัติขึ้นอยู่กับ: RAM, OS (32-bit vs 64-bit), Processor ขีดจำกัดทางทฤษฎี: sys.maxsize (2⁶³-1 บน 64-bit) แต่จะเจอ MemoryError ก่อนถึงขีดนี้ การจัดการไฟล์ใ

  3. การหาขีดจำกัดความยาวชื่อไฟล์สูงสุดใน Python (os.statvfs)

    บาง Platform ขีดจำกัดความยาวชื่อไฟล์ (Filename Length Limit) แตกต่างกันไปตาม Filesystem ที่ Mount อยู่ ตัวอย่าง: ตรวจสอบ Limit บน Root และ /boot os.statvfs(/boot)[statvfs.F_NAMEMAX] 255 คำอธิบาย os.statvfs(path) คืน os.statvfs_result (named tuple) ข้อมูล Filesystem statvfs.F_NAMEMAX = Index ของ

  4. การ Copy ไฟล์ระหว่าง Server ใน Python (SCP)

    คล้ายบทก่อนหน้า: ใช้ scp command ผ่าน subprocess หรือ paramiko + scp module วิธีที่ 1: subprocess + scp import subprocess import os p = subprocess.Popen([scp, my_file.txt, username@server:/path]) sts = os.waitpid(p.pid, 0) # รอ copy เสร็จ # Python 3.5+ ใช้ run() สะดวกกว่า subprocess.run([scp, my

  5. การ Copy ไฟล์ไป Remote Server ใน Python (SCP/SSH)

    มี 2 วิธีหลัก: ใช้ scp command ผ่าน subprocess หรือใช้ paramiko + scp module วิธีที่ 1: subprocess + scp command (ง่าย ไม่ต้อง library เพิ่ม) import subprocess import os p = subprocess.Popen([scp, my_file.txt, username@server:/remote/path]) sts = os.waitpid(p.pid, 0) # รอจน copy เสร็จ # หรือใช้

  6. การใช้ FTP ใน Python (ftplib)

    โมดูล ftplib ใน Standard Library ใช้เขียนโปรแกรมทำ FTP Jobs แบบอัตโนมัติ: เชื่อมต่อ FTP Server, ดึงไฟล์, ประมวลผลในเครื่อง ตัวอย่างพื้นฐาน import ftplib ftp = ftplib.FTP(ftp.yourserver.com, yourusername, your@email.address) print(File List:) files = ftp.dir() print(files) ftp.cwd(/tmp) # เปลี่ยน

  7. การรันคำสั่งต่างๆ ผ่าน SSH ใน Python (Paramiko exec_command)

    ใช้ Paramiko เดียวกับบทก่อนหน้า แต่เน้นการ รันหลายคำสั่ง ผ่าน SSH Connection เดียว การเชื่อมต่อ (เหมือนเดิม) from paramiko import SSHClient ssh = SSHClient() ssh.load_system_host_keys() ssh.connect(user@server:path) รันคำสั่งหลายคำสั่ง # คำสั่งเดี่ยว stdin, stdout, stderr = ssh.exec_command(ls -l

  8. วิธีง่ายที่สุดใช้ SSH ใน Python ด้วย Paramiko

    วิธีที่ง่ายที่สุดคือใช้ Library Paramiko (Pure Python SSH2 implementation) ติดตั้ง $ pip install paramiko ข้อกำหนดก่อนใช้งาน ตั้งค่า SSH Keys บน Host Machine ให้ถูกต้อง เมื่อรันสคริปต์ ต้องเข้าถึง Private Key ได้ ตัวอย่างเชื่อมต่อและรันคำสั่ง from paramiko import SSHClient ssh = SSHClient() ss

  9. Regular Expression (Regex) ใน Python คืออะไร?

    Regular Expression (Regex) คือลำดับของอักขระที่กำหนด Search Pattern ใช้สำหรับค้นหา จับคู่ แยก และแทนที่ข้อความในสตริงหรือไฟล์ รองรับในภาษาโปรแกรมส่วนใหญ่ (Python, Perl, Java, JavaScript, R, Go, ฯลฯ) หลักการพื้นฐาน ทุกอย่างคือ อักขระ (Character) เขียน Pattern เพื่อจับคู่ ลำดับอักขระเฉพาะ (String) ใ

  10. ฟังก์ชัน match() ใน Python Regex (re.match)

    re.match(pattern, string) พยายามจับคู่ pattern เฉพาะที่ตำแหน่งเริ่มต้นของสตริง เท่านั้น ตัวอย่าง: จับคู่ได้ (อยู่ต้นสตริง) result.group(0) TP ตัวอย่าง: จับคู่ไม่ได้ (ไม่อยู่ต้นสตริง) print(result) None การใช้งาน Match Object m.groups() (Hello, World) Raw String (r...) ใช้ rpattern (raw string

  11. ฟังก์ชัน search() ใน Python Regex (re.search)

    re.search(pattern, string) ค้นหา pattern ทุกตำแหน่งในสตริง คืน Match object ของการจับคู่ ครั้งแรก ที่เจอ ตัวอย่าง result.group(0) Tutorials คุณสมบัติสำคัญ ค้นหา ทั้งสตริง (ไม่จำกัดที่ต้นสตริงเหมือน match()) คืน Match object แรก ที่เจอ (Leftmost match) หาไม่เจอ → คืน None เข้าถึงข้อมูล Match Ob

  12. ความแตกต่างระหว่าง re.search และ re.match ใน Python

    ทั้ง re.match() และ re.search() เป็นเมธอดของโมดูล re แต่มีขอบเขตการค้นหาต่างกัน re.match() - จับคู่เฉพาะต้นสตริง ค้นหา pattern เฉพาะที่ตำแหน่งเริ่มต้นของสตริง เท่านั้น re.match(rTutorials, TP Tutorials Point TP) None # ไม่เจอ เพราะไม่ได้อยู่ต้นสตริง re.search() - จับคู่ทุกตำแหน่ง ค้นหา pattern ท

  13. การค้นหาและแทนที่ข้อความในไฟล์ด้วย Python (re.sub)

    โมดูล re มีฟังก์ชัน re.sub() สำหรับค้นหา pattern และแทนที่ด้วย substring ใหม่ หาไม่เจอ → คืนสตริงเดิม ไวยากรณ์ re.sub(pattern, repl, string, count=0, flags=0) pattern - regex pattern repl - สตริงแทนที่ (หรือฟังก์ชัน) string - สตริงต้นทาง count - จำนวนครั้งสูงสุดที่แทนที่ (0 = ทั้งหมด) flags - rege

  14. Regular Expression Modifiers (Flags) ใน Python

    Regular Expression Modifiers (Flags) ควบคุมพฤติกรรมการจับคู่ ระบุผ่านพารามิเตอร์ flags หรือรวมหลาย flag ด้วย | (bitwise OR) รายการ Flags ในโมดูล re Flagชื่อเต็มความหมาย re.IIGNORECASECase-insensitive matching re.LLOCALEใช้ Locale ปัจจุบันสำหรับ \w, \W, \b, \B (Legacy, ไม่แนะนำ) re.MMULTILINE^ จ

  15. การเปรียบเทียบสตริงแบบ Case-insensitive ใน Python

    วิธีมาตรฐาน: แปลงทั้งสองสตริงให้เป็นพิมพ์เล็ก (หรือพิมพ์ใหญ่) ก่อนเปรียบเทียบ ตัวอย่าง string1 = Star Wars string2 = star wars if string1.lower() == string2.lower(): print(The strings are case insensitive) else: print(The strings are not case insensitive) ผลลัพธ์ The strings are case inse

  16. การจับคู่ Start และ End ใน Python Regex (Anchors + Word Boundary)

    จับคู่ส่วนของสตริงที่ เริ่มต้นด้วย T และจบด้วย y โดยใช้ Anchors และ Word Boundary ตัวอย่าง import re result = re.findall(r\bT\S+y\b, TestCountry Hello) print(result) ผลลัพธ์ [TestCountry] คำอธิบาย Pattern \bT\S+y\b \b - Word boundary (ขอบเขตคำ) T - ตัวอักษร T ตรงตัว \S+ - Non-whitespace 1+ ตัว (

  17. การแบ่งสตริงด้วย Line Break หรือ Period ด้วย Python Regex

    ใช้ re.findall() กับ Regex pattern ที่จับคู่ข้อความระหว่าง Period (.) และ Newline (\n) ตัวอย่าง import re s = Hi. Its nice meeting you. My name is Jason. result = re.findall(r[^\s\.][^\.\n]+, s) print(result) ผลลัพธ์ [Hi, Its nice meeting you, My name is Jason] คำอธิบาย Pattern [^\s\.][^\.\n]+ [

  18. การพิมพ์สตริงซ้ำ 2 ครั้งใน Statement เดียวใน Python (* Operator)

    ใช้オペอเรเตอร์ * (asterisk) กับสตริง เพื่อ Repeat (ซ้ำ) สตริงนั้นๆ จำนวนครั้งที่ระบุ ตัวอย่าง print(string * 2) abcdefghijabcdefghij รูปแบบทั่วไป string * n # n = จำนวนเต็ม ≥ 0 ตัวอย่างเพิ่มเติม (1, 2) * 3 (1, 2, 1, 2, 1, 2) การประยุกต์: สร้าง Separator Line print(= * 50) ====================

  19. การพิมพ์สตริงต่อกัน (Concatenation) ใน Python

    ใช้オペอเรเตอร์ + (plus) เพื่อ Concatenate (ต่อ) สตริงสองสตริงเข้าด้วยกัน ตัวอย่าง print(s1 + s2) TutorialsPoint Hyderabad การต่อหลายสตริง .join([a, b, c, d]) abcd ข้อจำกัดของ + ต่อได้เฉพาะ String กับ String เท่านั้น hello + 123 → TypeError ต้องแปลงก่อน: hello + str(123) ประสิทธิภาพ: + vs join

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:30/451  20-คอมพิวเตอร์/Page Goto:1 24 25 26 27 28 29 30 31 32 33 34 35 36