Python ဖိုင်ကိုရေးပါ။
ရှိပြီးသားဖိုင်တစ်ခုသို့ စာရေးပါ။
ရှိပြီးသားဖိုင်တစ်ခုသို့ စာရေးရန်၊
open()
လုပ်ဆောင်ချက်တွင် ပါရာမီတာတစ်ခုကို ထည့်ရပါမည်။
"a"
- နောက်ဆက်တွဲ - ဖိုင်၏အဆုံးတွင် ထပ်ထည့်ပါမည်။
"w"
- Write - ရှိပြီးသားအကြောင်းအရာမှန်သမျှကို overwrite လုပ်ပါမည်။
ဥပမာ
"demofile2.txt" ဖိုင်ကိုဖွင့်ပြီး ဖိုင်တွင် အကြောင်းအရာကို ထပ်ထည့်ပါ-
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
#open and read the file after the appending:
f =
open("demofile2.txt", "r")
print(f.read())
ဥပမာ
"demofile3.txt" ဖိုင်ကိုဖွင့်ပြီး အကြောင်းအရာကို ထပ်ရေးပါ-
f = open("demofile3.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()
#open and read the file after the appending:
f = open("demofile3.txt", "r")
print(f.read())
မှတ်ချက်- "w" နည်းလမ်းသည် ဖိုင်တစ်ခုလုံးကို overwrite လုပ်လိမ့်မည်။
ဖိုင်အသစ်တစ်ခုဖန်တီးပါ။
Python တွင် ဖိုင်အသစ်တစ်ခုဖန်တီးရန် open()
၊ အောက်ပါဘောင်များထဲမှတစ်ခုဖြင့် နည်းလမ်းကို အသုံးပြုပါ။
"x"
- ဖန်တီးသည် - ဖိုင်တစ်ခုဖန်တီးမည်ဖြစ်ပြီး ဖိုင်ရှိလျှင် အမှားတစ်ခုကို ပြန်ပေးပါမည်။
"a"
- နောက်ဆက်တွဲ - သတ်မှတ်ထားသောဖိုင်မရှိပါက ဖိုင်တစ်ခုဖန်တီးပါမည်။
"w"
- Write - သတ်မှတ်ထားသောဖိုင်မရှိပါက ဖိုင်တစ်ခုဖန်တီးပါမည်။
ဥပမာ
"myfile.txt" ဟုခေါ်သော ဖိုင်တစ်ခုကို ဖန်တီးပါ-
f = open("myfile.txt", "x")
ရလဒ်- ဗလာဖိုင်အသစ်တစ်ခုကို ဖန်တီးလိုက်ပါပြီ။
ဥပမာ
မရှိပါက ဖိုင်အသစ်တစ်ခု ဖန်တီးပါ-
f = open("myfile.txt", "w")