Create Text files in Python is very easy, you can do it using the following code:
import os file = open("/path/filename.txt", "w") file.write("First line" + os.linesep) file.write("Second line") file.close()
The open function accepts the path of the file to be created and the second argument is the mode in which the file will be opened if it exists, where “w” indicates that it is for writing and “r” that it will be read only.
The op.linesep variable will allow you to obtain the necessary characters to create the line break according to the operating system where you are executing this routine.
If you run the code you will get a result like the following:
The code generates a small file with the two lines that we added with the write function. Of course, in your code you will have to change the path of the file you are going to create and make sure you have write permissions to the destination folder. As you can see creating text files in Python is really easy.
Create text files in Python is easy but may require more complexity in your project.
See more documentation: https://docs.python.org/3/tutorial/inputoutput.html
More examples: https://geekole.com/python/