Nearby lessons
80 of 108Python - Create Files
Detailed Tutorial Notes
Eg:
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
D:\Python_classes>py test.py File Name: abc.txt File Mode: w Is File Readable: False Is File Writable: True Is File Closed : False Is File Closed : True
Writing data to text files:
We can write character data to the text files by using the following 2 methods.
write(str)
writelines(list of lines)
Eg:
1) f=open("abcd.txt",'w')
2) f.write("Durga\n")
3) f.write("Software\n")
4) f.write("Solutions\n")
5) print("Data written to the file successfully")
6) f.close()abcd.txt:
Durga
Software
Solutions
Note: In the above program, data present in the file will be overridden everytime if we
run the program. Instead of overriding if we want append operation then we should open
the file as follows.
f = open("abcd.txt","a")
🧠 Test Your Knowledge
4 QuestionsProgress: 0 / 4
Keep Going!Python - Delete Files