Introduction
File operation is necessary in every programming.In Python, file handling is comparatively easier than the other language.
Reading A File
For reading a file, you can use open built-in method with or without "r" mode as a second arguments.
Program Code
finput = open("lola.txt")
print finput
data = finput.read()
print data
print finput
data = finput.read()
print data
Output
<open file 'none.txt', mode 'r' at 0x00B34440>
Welcome to Python world. Python is a nice language. It's fun to learn. And it's easy to use.
Welcome to Python world. Python is a nice language. It's fun to learn. And it's easy to use.
Writing To A File
For writing to a file, you also use built in open method with the mode "w". For example
Program Code
foutput = open("lola.txt", "w")
foutput.write("Hello From Python.")
foutput.write("We love to do what the best.")
foutput.close()
foutput.write("Hello From Python.")
foutput.write("We love to do what the best.")
foutput.close()
No comments:
Post a Comment