Dosya Okuma İşlemi # Python

in #blog5 years ago

Amatör olarak kodlama yapmaya devam ediyorum. Bugün dosya okuma işlemi ile alakalı biraz denemeler yaptım.


Source

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.

ayrc.png

I appreciate your upvotes and support

thankyou-e1485835939803.png

Sort:  

Congratulations @bpn! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You received more than 10 upvotes. Your next target is to reach 50 upvotes.

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! banana-small.png

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.

Coin Marketplace

STEEM 0.19
TRX 0.13
JST 0.029
BTC 65753.03
ETH 3281.80
USDT 1.00
SBD 2.68