Module-8 (File Handling)
Module-8 (File Handling)
Files input/output
Content
• Printing to the Screen
• Reading Keyboard Input
• The input Function
• Opening and Closing Files
• The open Function
• The file Object Attributes
• The close() Method
• Reading and Writing Files
• The write() Method
• The read() Method
File Open
f = open("demofile.txt", "r")
print(f.readline())
print(f.readline())
Loop
#Loop through the file line by line:
f = open(“test.txt", "r")
for x in f:
print(x)
f = open("demofile.txt", "r")
print(f.readline())
f.close()
# To delete a file, you must import the OS module, and run its
#os.remove() function.
#Remove the file “test.txt":
import os
os.remove("demofile.txt")
#To delete an entire folder, use the os.rmdir() method.
import os
os.rmdir(“foldername")
Thank you