Dosya Okuma İşlemi # Python
Amatör olarak kodlama yapmaya devam ediyorum. Bugün dosya okuma işlemi ile alakalı biraz denemeler yaptım.
Python’da değişkenler birer nesnedir. Değişken dediğimiz şey hafızadaki bir veri hücresi olduğuna göre hafızada tutulabilecek her şey değişkenlerle ifade edilebilir.
Mesela bir tam sayıyı, bir stringi, bir listeyi, bir fonksiyonu ya da bir dosyayı bir değişkene atayabiliriz.
Bu kadar açıklamadan sonra bir örnek vermeden olmaz.
Dosya Okuma
Bilgisayarda notepad programını kullanarak 2 adet dosya oluşturdum: dosya1.txt ve dosya2.txt
Amacım bu iki dosyanın içeriğini karşılaştırıp farklı olanları ekrana yazdırmak.
Dosya okuma işlemi yapmak için Python’da open() adında bir fonksiyon bulunuyor. Fonksiyon ismini yazdıktan sonra içerisine parametre olarak okunacak dosyanın adını yazıyoruz.
Kodu aşağıdaki gibidir :
d1 = open("C:/Users/bpn/Desktop/dosya1.txt") # dosyayı açıyoruz
d1_satırlar = d1.readlines() # satırları okuyoruz
print(d1_satırlar)
d2 = open("C:/Users/bpn/Desktop/dosya2.txt")
d2_satırlar = d2.readlines()
print(d2_satırlar)
print(type(d1_satırlar))
for i in d2_satırlar:
if not i in d1_satırlar:
print(i)
d1.close()
d2.close()
readlines() metodu ile ise dosyanın içeriğini komple bir şekilde satır satır almamızı sağlıyor.
Her iki dosyayı da open() fonksiyonu ile açıp readlines() metodu ile içerik okuması yaptım.
Sonraki for() döngüsü ve içerisindeki if() yapısı ile karşılaştırma yapıp farklı olan satırları ekrana yazdırdım.
Program bitmeden önce close() metodu ile dosyaları kapatmak da oldukça önemli. Unutmamak lazım.
Şimdilik bu kadar. Programı farklı alternatifler deneyerek test edebilirsiniz.
I appreciate your upvotes and support
How can I accept Jesus Christ in my life? Comment what you understand of our topic.
Check our Discord 😍🤗❤
Congratulations @bpn! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :
You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
Vote for @Steemitboard as a witness to get one more award and increased upvotes!
This post has received a $100.00 % upvote from @siditech thanks to: @addo.sports.
Here's a banana!
Thank you for using @sleepagent bid bot serves.
This post has received a 33 % upvote from @sleepagent.
You got a 50.00% promotion boost from @shares courtesy of @addo.sports! Thank you for following @Shares rules of promoting your articles.
Have a nice day.
@Shares - Keep steeming good articles to help Steem network grow.
You got a 50.00% upvote from @whalecreator courtesy of @addo.sports! Delegate your Steem Power to earn 100% payouts.
You got a 50.00% upvote from @redlambo courtesy of @addo.sports! Make sure to use tag #redlambo to be considered for the curation post!
You got a 50.00% upvote from @whalepromobot courtesy of @addo.sports!
You got a 33.33% upvote from @bid4joy courtesy of @addo.sports!
you just rose a upvote from @curationhelper thanks for post promotion.