Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีลบ Swap Files ด้วย Python

Swap Files มี Extension .swp วิธีง่ายที่สุดในการลบ Swap Files ทั้งหมดจากโฟลเดอร์แบบ Recursive คือใช้ String Function endswith กับ Extension Name (.swp) เพื่อ Match ชื่อไฟล์และลบไฟล์เหล่านั้น

ตัวอย่าง

import os, os.path

mypath = "my_folder"
for root, dirs, files in os.walk(mypath):
    for file in filter(lambda x: x.endswith('.swp'), files):
        os.remove(os.path.join(root, file))

โปรแกรมนี้จะค้นหาใน Directory "my_folder" แบบ Recursive และลบไฟล์ทั้งหมดที่ลงท้ายด้วย .swp