File handling in the python

technical talkiess
0

  mode to open files in the python


"""
"r" - Reading mode -default mode
"w" - Write mode
"x" - file not exists then create file
"a" - add more text in the file
"t" - text mode - default mode
"b" - binary mode
"+" - Read + write mode
"""
open the file
f = open("text.txt","rt")  
f.read function read all the document in the file  
content = f.read()
f.read(2) the read function read only the 2 character in the file
content = f.read(2)
print(content)
line by line execution perform to the below code
for line in f:
    print(line)

the above execution the one new line added you can remove the new line putting the end in the print statement
for line in f:
    print(line,end="")

if you want print line of the text in the file you can direct use ... the below function is print only first line of the file


content = f.readline()

if you want next line ... declare the variable in the second time

content = f.readline()
content = f.readline()

print(content)

If you want to all text of the file in the list ... then you can use the following function

content = f.readlines()
print(content)


text.txt:-

vishal is the smart boy vishal education bsc it complete asd asdasd asd asdas asdd as asd

Post a Comment

0Comments

Thanks you for commenting your questions. I will see question and respond you.

Post a Comment (0)