► Python program to create a text file and write something in it


Steps followed:
1. first open a file with name test_file.txt in write mode
2. create a variable sometext with value "this is our test file"
3. write sometext variable value in the test_file.txt
 


Program

#Python program to create a text file and write something in it

#opening a file with name test_file.txt in write mode
with open("test_file.txt","w") as myfile:
    #create a variable sometext with value "this is our test file"
    sometext="this is our test file"
    #write sometext variable value in the test_file.txt
    myfile.write(sometext)
 


Output

the above program will create a file with name  test_file.txt and content as "this is our test file"





Also Read: