python file object - edit File I/O with documentation

Advance-File I/O

 

                                  

First we learn how to do rename any python file. python language does not support directly to change any file name. Secondary ways to rename python file to edit his name.

with open("this.txt") as f:
    content = f.read()

with open ("copythis.txt","w") as f:
    f.write(content)

Here first we open the file in read mode this.txt. make content variable and content save all this.txt proporties. Again open here copythisfile.txt  with write mode write content in c copythis.txt. Then you can delete the file this.txt. This is the process to makefile rename

oldname = "sampleme.txt"
mewname = "samplenew.txt"

with open(oldname) as f:
    content = f.read()

with open(mewname,"w") as f:
    f.write(content)

you can also change filename means rename in this process.

Change specific word from txt file 

words = ["sam","hari"]
with open("sampleme.txt") as f:
    content = f.read()
    


for  word in words:
    content = content.replace("word","sami is official good boy")
    with open("sampleme.txt","w") as f:
        f.write(content)

In this program we change or replace specific word. Make a list with included thats words which we want to replace. Open sample.txt as read mode. blank mode is always denote read mode. Content variable reads sample.txt file. 

Now we will make loop to find that words to replace it. All read file saved in content so content is replace new word with old word. all file again wite and print console. there is change in sampleme.txt. 

It is a quize you wan copy this code and paste your code editor and comment in below work or not work

with open("sampleme.txt") as f:
    content = f.read()
    content = content.replace("sam","samir")
    with open("sampleme.txt","w") as f:
        content = f.write(content)

Don't forget to comment this word works or not work.

practice problem of python

First problem we make automatically generate multiplication table

for i in range(2,21):
    with open(f"multiplication table of class1 to class5{i}","w") as f:
        for j in range(1,11):
            f.write(f"{i}x{j}={i*j}\n")

Here we first make for loop give range 2,21 last 21 is -1 so practically range is 2 ,20. With open file multiplication table class1 to  class 5. Again make range is 1 ,11. write i*j in multiplication table of class1 to class5.


mypoem = open("twinkale.txt")
this = mypoem.read()
if "twinkale" in this:
    print("TWINKALE IS PRESENT")
else:
    print("twinkale is not present")
print(this)

mypoem.close
Make mypoem varible to open twinkale.txt and this this variable save mypoem all data. Use if condition to find twinkle from this this means mypoem save data. if twinkale find in this  then print TWINKALE IS PRESENT . 
else not find twinkale then print  twinkale not present.then print this to chect our program work correctly or not . At last close mypoem.
Why at last close mypoem. Because if we oen file with open condition then close mypoem file.. so, here we open file without with open  condetion that why we write our program file close condition.

def mygame():
    return 101

score = mygame()
with open("highscore.txt") as f:
    highscorestr = int(f.read())


if highscorestr (greatersigh) scoreor highscorestr=='':
    with open("highscore.txt","w") as f:
        f.write(str(score))

Greatersigh means '<' due to javascrip error we cannot write ther greater than sigh.



Post a Comment

0 Comments