โมดูล Glob เป็นหนึ่งในโมดูลมาตรฐานของ Python สามารถค้นหาไฟล์ตามรูปแบบที่กำหนดได้
ในการใช้งานให้ import glob เข้ามาครับ ส่วนคำสั่ง สามารถใช้งานง่าย ๆ ด้วยคำสั่ง
glob.glob()
ตัวอย่างการใช้งาน
>>> import glob
>>> glob.glob('*')
['add2virtualenv.bat', 'boost_python-wininst.log', 'cd-.bat','work1.txt',...]
>>> glob.glob('[a-z]????.*') #แสดงรายการแฟ้มทั้งหมดที่เริ่มต้น ด้วยตัวอักษรตามด้วยตัวอักษรอีก 4 ตัว (ตัวเลข ตัวอักษร)
['work1.txt']
>>> glob.glob('*.exe') #ค้นตามรูปแบบที่กำหนด คือ ค้นไฟล์ที่มีนามสุกล .exe
['python.exe', 'pythonw.exe', 'Removeboost_python.exe', 'Removecx_Freeze.exe', 'RemoveCython.exe', 'Removeipython.exe', 'Removelxml.exe', 'Removenumpy.exe', 'RemovePillow.exe', 'Removepsutil.exe', 'Removepy2exe.exe', 'RemovePyAudio.exe', 'Removepycrypto.exe', 'Removepygame.exe', 'RemovePyICU.exe', 'RemovePySide.exe', 'RemovePyYAML.exe', 'Removescipy.exe']
ติดตามบทความต่อไปนะครับ
ขอบคุณครับ
0 ความคิดเห็น:
แสดงความคิดเห็น
แสดงความคิดเห็นได้ครับ :)