Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. วิธีใช้ file.readlines() อ่านหลายบรรทัดใน Python

    read() อ่านทั้งไฟล์ทีเดียว ใช้ readlines() อ่านทีละบรรทัด ตัวอย่าง f = open(my_file.txt, r+) for line in f.readlines(): print(line) f.close() แนะนำใช้ with statement ปิดอัตโนมัติ: with open(my_file.txt, r+) as f: for line in f.readlines(): print(line)

  2. วิธี Truncate ไฟล์ที่ขนาดกำหนดใน Python

    truncate([size]) ตัดขนาดไฟล์ ถ้าระบุ size จะตัดที่ size นั้น (สูงสุด) default คือ current position เรียก truncate() โดยไม่มี argument จะตัดที่ตำแหน่งปัจจุบัน ตัวอย่าง >>> # test.txt contents: ABCDE >>> f = open(test.txt, r+) >>> f.truncate(2) >>> f.read() AB หมายเ

  3. วิธีสร้างไฟล์ขนาดกำหนดใน Python

    สร้างไฟล์ขนาดเฉพาะ: seek ไปที่ byte ที่ต้องการ เขียน byte เดียว (sparse file) ตัวอย่าง: Sparse File 1 GB with open(my_file, wb) as f: f.seek(1024 * 1024 * 1024) # 1 GB f.write(b0) สร้างไฟล์เต็ม (จริงๆ ใช้พื้นที่): with open(my_file, wb) as f: num_chars = 1024 * 1024 * 1024 f.write(b

  4. วิธีเขียนบรรทัดเดียวลง Text File ใน Python

    ใช้ write เขียนบรรทัดเดียว: ตัวอย่าง f = open(myfile, w) f.write(hi there\n) # Python แปลง \n เป็น os.linesep เอง f.close() # ส่วนใหญ่ละได้ เพราะ destructor จะปิดให้ หรือใช้ print() (Python 2.6+): from __future__ import print_function # Python 2 เท่านั้น print(hi there, file=f) Python 3 ไม่ต้อง i

  5. วิธีเขียนหลายบรรทัดลง Text File ใน Python

    ใช้ write คั่นบรรทัดด้วย \n: ตัวอย่าง line1 = First line line2 = Second line line3 = Third line with open(my_file.txt, w) as out: out.write({}\n{}\n{}\n.format(line1, line2, line3)) หรือใช้ writelines: ตัวอย่าง line1 = First line line2 = Second line line3 = Third line with open(my_file.txt, w) a

  6. วิธีเปลี่ยน File Permissions ใน Python

    ใช้ os.chmod(file, mode) เปลี่ยน permission mode เป็น octal (ขึ้นต้น 0o) เช่น 0o777 >>> import os >>> os.chmod(my_file, 0o777) หรือใช้ flags จากโมดูล stat หรือ subprocess.call([chmod, 0444, my_file])

  7. วิธีตรวจสอบว่าไฟล์มีอยู่จริงหรือไม่ใน Python

    ใช้ os.access(path, mode) ตรวจสอบ permission และการมีอยู่ของไฟล์ ตัวอย่าง >>> import os >>> os.access(my_file, os.F_OK) # ตรวจสอบการมีอยู่ True >>> os.access(my_file, os.R_OK) # ตรวจสอบอ่านได้ True >>> os.access(my_file, os.W_OK) # ตรวจสอบเขียนได้ True >>

  8. วิธี Merge ไฟล์หลายไฟล์เป็นไฟล์ใหม่ใน Python

    เหมือนบทความก่อนหน้า: อ่านไฟล์แต่ละไฟล์ เขียนต่อในไฟล์ปลายทาง ตัวอย่าง (อ่านทั้งไฟล์) filenames = [file1.txt, file2.txt, file3.txt] with open(output_file, w) as outfile: for fname in filenames: with open(fname) as infile: outfile.write(infile.read()) ตัวอย่าง (อ่านทีละบรรทัด

  9. วิธีรวม (Concatenate) สองไฟล์ขึ้นไฟล์ใหม่ใน Python

    รวมหลายไฟล์เป็นไฟล์ใหม่: อ่านไฟล์แต่ละไฟล์ เขียนต่อท้ายในไฟล์ปลายทาง ตัวอย่าง (อ่านทั้งไฟล์ทีละไฟล์) filenames = [file1.txt, file2.txt, file3.txt] with open(output_file, w) as outfile: for fname in filenames: with open(fname) as infile: outfile.write(infile.read()) ตัวอย่าง (

  10. วิธีเปลี่ยน Mode (Permission) ของไฟล์ใน Python

    ใช้ os.chmod(file, mode) เปลี่ยน permission mode ต้องเป็น octal (ขึ้นต้นด้วย 0o) เช่น 0o777 >>> import os >>> os.chmod(my_file, 0o777) หรือใช้ flags จากโมดูล stat (รายละเอียด) อีกวิธี ใช้ subprocess เรียก chmod: >>> import subprocess >>> subprocess.call([chmod, 04

  11. วิธีปิดไฟล์ที่เปิดอยู่ทั้งหมดใน Python

    Python ไม่มีวิธี native track ไฟล์ที่เปิดอยู่ทั้งหมด ต้อง track เอง หรือใช้ with statement ที่ปิดอัตโนมัติ ตัวอย่าง with statement with open(file.txt) as f: # do something with f here หรือสร้าง class จัดการไฟล์หลายไฟล์: ตัวอย่าง Class class OpenFiles: def __init__(self): self.files =

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