1file = open(“testfile.txt”,”w”)
2
3file.write(“Hello World”)
4file.write(“This is our new text file”)
5file.write(“and this is another line.”)
6file.write(“Why? Because we can.”)
7
8file.close()
1with open("hello.txt", "w") as f:
2 f.write("Hello World")
3#using With Statement files opened will be closed automatically
1#Write a Python program to create a file of numbers by taking input from the user and then display the content of the file. You can take input of non-zero numbers, with an appropriate prompt, from the user until the user enters a zero to create the file assuming that the numbers are non-zero.
2 f = open ('NumFile.txt','w')
3 while True :
4 no = int(input("enter a number (0 for exit)"))
5 if no == 0 :
6 print("you entered zero(0) ....... \n now you are exit !!!!!!!!!!!")
7 break
8 else :
9 f.write(str(no)+"\n")
10 f.close()
11 f1 = open ('NumFile.txt','r')
12 print("\ncontent of file :: \n",f1.read())
13 f1.close()
14
15