# Controlling files with Python

ဒီတစ်ပတ်ပြောပြပေးသွားမှာကတော့ file I/O ပဲဖြစ်ပါတယ်။

File I/O ဆိုတာက file တွေကို input/output လုပ်ပေးတာကိုခေါ်တာဖြစ်ပြီး File ကိုသုံးတယ်ဆိုရင်  တခြား list တွေ dictionary တွေနဲ့မတူပဲ informationတွေ value တွေကို အချိန်အကြာကြီး ထိန်းထားနိုင်မှာဖြစ်ပါတယ်။

File တွေကိုဖွင့်ချင်ရင် Open လို့သုံးရပြီး ပိတ်ချင်တယ်ဆိုရင်တော့ Close အနေနဲ့သုံးရမှာဖြစ်ပါတယ်။ File နဲ့တွဲသုံးတာတွေက "w", "a", "r" တွေဖြစ်တယ်။ w က အဲ့ file ပေါ်မှာ value အသစ်တွေရေးတာ၊ a က append လုပ်တာဖြစ်ပြီး၊ r က တော့ read လုပ်တာဖြစ်တယ်။

```python
file = open("names.txt", "w")

file.write("Hello")

file.close()
```

with ဆိုတာလေးနဲ့သုံးရင်တော့ ကိုယ်က close လုပ်စရာမလိုပဲ သူ့ဟာသူ close လုပ်ပေးသွားမှာပါ။

```python
with open(filename) as file:
    ...
```

csv ဆိုတာက file type တစ်ခုပါ။ တခြား file type တွေဖြစ်တဲ့ .txt , .py တို့နဲ့အတူတူပါပဲ။ .txt ဆိုရင်စာတွေပဲရိုက်လို့ရတဲ့ဖိုင်တစ်ခုဖြစ်ပြီး .py ကတော့ python file ပါ။. csv ရဲ့အရှည်ကတော့ comma separated values ပဲဖြစ်ပါတယ်။ သူကတော့ information တွေအများကြီးသိမ်းနိုင်ပြီးတော့ column နဲ့ row ဆိုပြီးယူပါတယ် excel လိုပေါ့။ csv fileလို့ပြောတဲ့နေရာမှာ string အနည်းငယ်သာမက  paragraph လိုက်ကြီးတွေပါ ရေးသားနိုင်ပါတယ်။ csv file ကို ပိုသပ်သပ်ရပ်ရပ်ဖြစ်စေချင်ရင်တော့ File ရဲ့ အပေါ်ဆုံး row မှာ column ရဲ့ နာမည်တွေကို comma လေးတွေခြားပြီး ပေးထားလို့ရပါတယ်။

For example;

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1706537541007/22e5e2b7-a885-4e7e-bfed-8a218adea201.png align="left")

ဒီမှာဆိုရင်တော့ Name က column တစ်ခု House က column တစ်ခုဖြစ်ပါတယ်။ comma တွေက column တွေကိုခြားပေးတဲ့ ကြားက line လေးတွေပေါ့။ နောက်ပြီးတော့ .split() ဆိုတဲ့ function အကြောင်းကိုဆက်ပြောချင်ပါတယ်။ .split() ဆိုတာက string တစ်ခုကိုဘယ်နေရာကနေ ခွဲထုတ်မလဲဆိုတာပါ။ ခွဲထုတ်ပြီးရင်လဲ list အနေနဲ့ return ပြန်ပါတယ်။ ပုံမှန် .split() ဆိုရင်တော့ space တွေကနေခွဲမှာပဲဖြစ်ပါတယ်။ ဒါမှမဟုတ် .split(",") ကတော့ comma ကနေခွဲထုတ်မှာပဲဖြစ်ပါတယ်။

.sorted() ဆိုတာကတော့ ငယ်စဥ်ကြီးလိုက်ဖြစ်စေကြီးစဥ်ငယ်လိုက်ဖြစ်စေ sort လုပ်ပေးတဲ့ function ပဲဖြစ်ပါတယ်။ sort လုပ်မယ်ဆိုရင်သူက argument လိုပါတယ်။ ငယ်စဥ်ကြီးလိုက်(ascending)စီမယ်ဆိုရင်တော့ ဒီအတိုင်းထည့်ရေးလိုက်။ ကြီးစဥ်ငယ်လိုက်(descending)စီချင်ရင်တော့ argument ရဲ့နောက်မှာ , နဲ့ reverse = True ဆိုပြီးလုပ်ပေးရပါမယ်။

key ဆိုတဲ့ parameter အကြောင်းကိုပြောချင်ပါတယ်။ key ဆိုတာက parameter တစ်ခုဖြစ်ပြီးသူက function တစ်ခုကို argument အနေနဲ့တခြား function တစ်ခုမှာခေါ်သုံးလို့ရပါတယ်။ နောက်ပြီး lambda ဆိုတဲ့ keyword ကိုလဲ key နဲ့တွဲသုံးလို့ရပါတယ်။ lambda ကတော့ function ဆောက်မထားတဲ့အခြေအနေတွေမှာသုံးလို့ရပြီး python ကသူ့ကို function တစ်ခုအနေနဲ့ သက်မှတ်သွားမှာပါ။ name မရှိတဲ့ function တစ်ခုလို့ အလွယ်မှတ်လို့ရပါတယ်။ python မှာ csv ဆိုတဲ့ module လဲရှိပါသေးတယ်။ အဲ့ module ထဲကတစ်ခုဖြစ်တဲ့ .reader() က open ဆိုတဲ့ function နဲ့ဖွင့်ထားတဲ့ file ထဲကရှိတဲ့အရာအားလုံးကို read လုပ်သွားမှာဖြစ်ပါတယ်။

csv (Comma Separated Values) library မှာဆိုရင် တည်ဆောက်ထားတဲ့ csv fileကို ဖတ်ပေးပြီး comma (,) ဘယ်နေရာမှာရှိတယ်၊ quote ( ‘ ‘ or “ ”) ဘယ်နေရာမှာရှိတယ်ဆိုတာကိုသိရှိနိုင်တဲ့ .reader() ဆိုတဲ့ method ပါရှိပါတယ်။ csv.reader() သုံးထားတဲ့ file ကို for loop ပတ်လို့လည်းရပါတယ်။ csv fileကို for loop ပတ်တဲ့အချိန်မှာ row တစ်ခုကို တစ်ကြိမ်ပတ်ပါတယ်။ အလွယ်ပြောရရင် ဘယ်ကနေ ညာကိုသွားပါတယ်။ . reader() method က အဲ့ဒီ row မှာပါတဲ့ comma တွေအလိုက် element တွေကို split လုပ်ပေးပါတယ်။

csv.DictReader() ဆိုတဲ့ method ကတော့ ‌column‌ တွေကို dictionary ပုံစံဖွဲ့စည်းပေးပါတယ်။ csv file ထဲမှာ User input တောင်းထားတဲ့ data တွေကို csv.writer() သုံးပြီး append လုပ်လို့ရပါသေးတယ်။ appendလုပ်မယ်ဆိုရင်တော့ csv file ဖွင့်တဲ့နေရာမှ “w”, rတို့ အစား “a” ကိုသုံးပေးရမှာဖြစ်ပါတယ်။

File တွေက စာတွေပဲရိုက်လို့ရတာလား။ NO! အခုပြောမယ့်ဟာ‌လေးကတော့ Binary File လို့ခေါ်ပါတယ်။ binary file ဆိုတာက 0 တွေ 1 တွေ စုထားတာကိုခေါ်ပါတယ်။ သူတို့က music ရော image တွေရောကို store လုပ်နိုင်ပါတယ်။ ဒီ file လေးတွေနဲ့တွဲသုံးလို့ တော်တော်လေးအဆင်ပြေတဲ့ PIL ဆိုတဲ့ library အကြောင်းလဲပြောချင်ပါတယ်။ ဒီ library နဲ့ဆိုရင် ကိုယ့်ဘာကိုယ် animated GIF လေးတွေလုပ်လို့ရပါတယ်။ library ရဲ့အသေးစိတ်ကိုတော့

Fun Facts: Animated GIFs are a popular type of image file that has many image files within it that are played in sequence over and over again, creating a simplistic animation or video effect.

Thank you so much for reading &gt;&lt;

---

***Weekend Warriors***

* 6 members : 1 girl and 5 boys
    
    * 14 to 18 years old
        
    * learning programming for a year
