#python tutorial,#python full course,#python programming,#pythonmc,#python projects,#python telusko,#python snake,#python tutorial for beginners,#python course,#python apni kaksha,#python automation,#python attack,#python app,#python api,#python algorithms,#python advanced,#python anaconda
How to Create a Text File
With Python you can create a .text files (guru99.txt) by using the code, we have demonstrated here how you can do this
Step 1)
f= open("success learners.txt","w+")
We declared the variable f to open a file named success learners.txt. Open takes 2 arguments, the file that we want to open and a string that represents the kinds of permission or operation we want to do on the file
Here, we used "w" letter in our argument, which indicates write and will create a file if it does not exist in library
Plus sign indicates both read and write.
The available option beside "w" are, "r" for read, and "a" for append
Step 2)
for i in range(10):
f.write("This is line %d\r\n" % (i+1))
We have a for loop that runs over a range of 10 numbers.
Using the write function to enter data into the file.
The output we want to iterate in the file is "this is line number", which we declare with write function and then percent d (displays integer)
So basically we are putting in the line number that we are writing, then putting it in a carriage return and a new line character
Step 3)
f.close()