as many other interpreted languages, python has a verity of builtin functions to perform different tasks.
here are some ways to write, read and print a file using python.
Learn Web Technologies for Free. Learn C#, LINQ, ASP.NET MVC, .NET Core, jQuery, JavaScript, Angular, Node.js, HTTPS, Python, Sass, D3.js, and many more latest technologies using easy tutorials.
as many other interpreted languages, python has a verity of builtin functions to perform different tasks.
here are some ways to write, read and print a file using python.
# Program to show various ways to read and
write data in a text file. file1
=
open
("textfile.txt","w")
L
=
["This
is
Multan \n","This
is
Lahore \n","This
is
Karachi \n"]
# \n is placed to indicate EOL (End of Line)
file1.write("Hello web-healer.blogspot.com \n")
file1.writelines(L)
file1.close()
#to change file access modes
file1
=
open
("txtfile.txt","r
+
")
print
("Output of Read function
is.
")
print
(file1.read())
print
()
# seek(n) takes the file handle to the nth value.
# bite from the beginning of the file.
file1.seek(
0
)
print
( "Output of Readline function
is.
")
print
(file1.readline())
print
()
file1.seek(
0
)
# To show difference between read and readline using python code
print
("Output of Read(
9
) function
is
")
print
(file1.read(
9
))
print
()
file1.seek(
0
)
print
("Output of Readline(
9
) function
is
")
print
(file1.readline(
9
))
file1.seek(
0
)
# readlines function
print
("Output of Readlines function
is
")
print
(file1.readlines())
print
()
file1.close()
No comments:
Post a Comment