Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. วิธี Copy ไฟล์ใน Python

    ใช้ shutil.copy(source, destination) จากโมดูล shutil คัดลอกไฟล์และโฟลเดอร์ได้ ตัวอย่าง >>> import shutil >>> shutil.copy(original.txt, duplicate.txt) /home/username/duplicate.txt >>> shutil.copy(original.txt, my_folder/duplicate.txt) /home/username/my_folder/duplicate.txt

  2. วิธี Copy ไฟล์ไปยังไดเรกทอรีใหม่ด้วย Python

    ใช้โมดูล shutil สำหรับ copy ไฟล์/โฟลเดอร์ สำหรับหลายไฟล์ ต้องมี list แล้ววนลูป copy shutil.copy(source, destination) copy ไฟล์จาก source ไป destination (ถ้า destination เป็นชื่อไฟล์ จะ rename) คืนค่าพาธไฟล์ใหม่ ตัวอย่าง import shutil, os files = [file1.txt, file2.txt, file3.txt] os.mkdir(my_new_fold

  3. วิธีดู System Configuration เกี่ยวกับ Open File ใน Python

    ใช้ fpathconf(file_descriptor, name) ดู system configuration ของ open file (เฉพาะ Unix) name ระบุค่าที่ต้องการ (เช่น PC_LINK_MAX, PC_NAME_MAX) ตัวอย่าง import os, sys fd = os.open(foo.txt, os.O_RDWR | os.O_CREAT) no = os.fpathconf(fd, PC_LINK_MAX) print(Maximum number of links to the file: %d % no)

  4. วิธีดู Stat ของไฟล์ใน Python

    ใช้ os.stat() ทำ stat system call บน path ที่กำหนด คืนค่า tuple 10 ค่า: import os st = os.stat(file.dat) # (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) รายละเอียดเพิ่มเติม: effbot.org

  5. วิธีดู Permission Mask ของไฟล์ใน Python

    ใช้ os.stat() เพื่อดู stat ของไฟล์ คืนค่า tuple 10 ค่า: import os st = os.stat(file.dat) # (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) mode (st[0]) มีข้อมูล permission อ่านเพิ่มเติมได้ที่: effbot.org

  6. วิธีดูข้อมูล File System ด้วย Python

    โมดูล os มี uname() สำหรับข้อมูล OS (เฉพาะ *NIX) แพลตฟอร์มข้ามได้ใช้ platform module: >>> import platform >>> platform.machine() AMD64 >>> platform.version() 10.0.15063 >>> platform.platform() Windows-10-10.0.15063-SP0 >>> platform.uname() uname_result(sy

  7. วิธี Force Write ไฟล์ลง Disk ด้วย File Descriptor ใน Python

    ใช้ fdatasync(fd) บังคับเขียนไฟล์ลงดิสก์ (ไม่รวม metadata) ใช้ได้เฉพาะ Unix แพลตฟอร์มข้ามได้ ใช้ fsync(fd) (Unix เรียก fsync() Windows เรียก _commit()) ตัวอย่าง import os, sys fd = os.open(foo.txt, os.O_RDWR | os.O_CREAT) os.write(fd, This is test) os.fsync(fd) # บังคับเขียนลงดิสก์ os.lseek(fd, 0,

  8. วิธีสร้าง Hard Link ของไฟล์ใน Python

    os.link(src, dst) สร้าง hard link ชื่อ dst ชี้ไปที่ src ใช้สำหรับสร้าง copy ของไฟล์ที่มีอยู่ ตัวอย่าง >>> import os >>> os.link(photo.jpg, my_photo.jpg) ตอน list ไฟล์จะเห็น my_photo.jpg

  9. วิธีดึง Major Device Number จาก Raw Device Number ใน Python

    os.major(device) ดึง major device number จาก raw device number (มักมาจาก st_dev หรือ st_rdev ของ stat) ตัวอย่าง import os, sys path = /var/www/html/foo.txt info = os.lstat(path) major_dnum = os.major(info.st_dev) print(Major Device Number:, major_dnum) ผลลัพธ์ Major Device Number: 0

  10. วิธีรวม Major/Minor Device Numbers เป็น Raw Device Number

    os.makedev(major, minor) รวม major และ minor device numbers เป็น raw device number ตัวอย่าง import os, sys path = /var/www/html/foo.txt info = os.lstat(path) major_dnum = os.major(info.st_dev) minor_dnum = os.minor(info.st_dev) dev_num = os.makedev(major_dnum, minor_dnum) print(Device Number:, dev_n

  11. วิธีสร้างและใช้ Named Pipe (FIFO) ใน Python

    FIFOs คือ pipe ที่เข้าถึงได้เหมือนไฟล์ทั่วไป มีอยู่จนกว่าจะลบ (เช่น os.unlink()) ใช้เป็น rendezvous point ระหว่าง client/server: server เปิดอ่าน client เปิดเขียน os.mkfifo() สร้าง rendezvous point ไม่ได้เปิด FIFO หากไฟล์มีอยู่แล้วจะ error ใช้ try-except กัน error: ตัวอย่าง import os, sys path = /tmp

  12. Query SAP Database ด้วย Python

    Python เป็น object-oriented language ที่เข้าใจง่าย สำหรับใช้กับ SAP ต้องติดตั้ง PyRFC (Python SAP RFC module) ซึ่งมีเมธอด RFC_READ_TABLE สำหรับอ่านข้อมูลจากตารางใน SAP database PyRFC มี bindings สำหรับเรียก ABAP ↔ Python ทั้งสองทาง สามารถกำหนด SAP data types สำหรับแลกเปลี่ยนข้อมูลได้ ยังสามารถสร้าง

  13. วิธีสร้าง Filesystem Node ด้วย Python

    os.mknod(path, mode=0o600, device=0, *, dir_fd=None) สร้าง filesystem node (ไฟล์, device special file, หรือ named pipe) ชื่อ path พร้อมกำหนด mode และ type ผ่าน bitwise OR กับค่าคงที่ใน stat (S_IFREG, S_IFCHR, S_IFBLK, S_IFIFO) ใช้ได้เฉพาะ Unix import os import stat filename = /tmp/tmpfile mode = 0o6

  14. วิธีเปิด Pseudo-terminal Pair ใหม่ใน Python

    ใช้ os.openpty() เปิด pseudo-terminal pair ใหม่ คืนค่า file descriptors (master, slave) ตัวอย่าง import os m, s = os.openpty() print(m) print(s) # แสดงชื่อ terminal s = os.ttyname(s) print(m) print(s) ผลลัพธ์ 3 4 3 /dev/pty0

  15. ฟังก์ชัน os.pipe() ใน Python ทำอะไร?

    os.pipe() สร้าง pipe และคืนค่า pair of file descriptors (r, w) สำหรับอ่านและเขียนตามลำดับ ตัวอย่าง: Process แบบ parent-child สื่อสารผ่าน pipe import os, sys print(The child will write text to a pipe and) print(the parent will read the text written by child...) r, w = os.pipe() processid = os.fork()

  16. วิธี Rename ไดเรกทอรีใน Python

    เปลี่ยนชื่อไดเรกทอรีโดย ย้าย (move) ด้วยโมดูล shutil หรือ os.rename ใช้ shutil.move >>> import shutil >>> shutil.move(my_folder, new_name) ใช้ os.rename >>> import os >>> os.rename(my_folder, new_name) ทั้งสองวิธีจะ rename my_folder เป็น new_name ได้

  17. วิธีสร้างชื่อไฟล์ชั่วคราวที่ไม่ซ้ำกันด้วย Python

    ใช้ tempfile.mkstemp() สร้าง temporary file แบบปลอดภ ไม่มี race condition ไฟล์อ่าน/เขียนได้เฉพาะ user ที่สร้าง import tempfile _, temp_file_path = tempfile.mkstemp() print(File path: + temp_file_path) หมายเหตุ: ต้องลบไฟล์นี้เองหลังใช้งานเสร็จ

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