os module ၏ path.getsize method

path.getsize method သည် file တစ်ခု သို့မဟုတ် folder တစ်ခု၏ အရွယ်အစားကို bytes ဖြင့် ပြန်ပေးပါသည်။ method ၏ parameter တွင် လိုအပ်သော file သို့မဟုတ် folder ၏ path ကို ထည့်သွင်းပေးရပါမည်။ သတ်မှတ်ထားသော path မရှိပါက OSError exception ကို ပြန်ပေးပါမည်။

Syntax

pythonimport os

print(os.path.getsize(path))

နမူနာ

ကျွန်ုပ်တို့၏ file ၏ အရွယ်အစားကို bytes ဖြင့် သိရှိကြပါစို့:

pythonimport os

print(os.path.getsize('file.txt'))

run ထားသော code ၏ ရလဒ်:

python210

နမူနာ

File ၏ အရွယ်အစားကို kilobytes ဖြင့် သိရှိကြပါစို့။ ထိုသို့ပြုလုပ်ရန် ရရှိလာသော တန်ဖိုးကို 1000 ဖြင့် စားပေးရုံသာဖြစ်ပါသည်:

pythonimport os

print(os.path.getsize('file.txt') / 1000)

Run ထားသော code ၏ ရလဒ်:

python0.21

ဆက်လက်လေ့လာရန်

  • disk_usage method of shutil module,
    which returns disk usage statistics
  • stat method of os module,
    which returns the status of a path
  • path.exists method of os module,
    which checks the existence of a path
  • path.isdir method of os module,
    which checks the existence of a directory
  • path.isfile method of os module,
    which checks the existence of a file