@Ivan, this isn't really a software engineering question. It would be better posted on the StackOverflow site which deals with questions about software development. There is an active Python community there that you can access. However since you are new and struggling with programming here are some tips.
In your example you aren't creating a file, you are just doing output with print. If you want to create a file you need to open the output file with open(filename, "w") specifying the "w" to indicate you are going to write to the file.
Python makes it easy to open a file and have it closed when you are finished using it. The with keyword creates a block that, when the program control flows out of that block closes the file.
If you want to write to three different files you will have to name them differently. The way to do this is create a variable and increment it each time you open a new file.
Python also makes it easy to read lines of text from a file. Calling in on a file within a for loop will give you the next line of a text fille. Note that the final \n character on the line is included in what is returned. In this case that is what you want since you are going to write it to your output file and file's write expects you supply the \n, unlike print which puts one on for you.
So, putting this all together your program may look like
outnum = 0
with open("test.txt") as infile:
outnum += 1
outfile = open(f"test_out{outnum}.txt", "w")
for line in infile:
if outfile.closed:
outfile = open(f"test_out{outnum}.txt", "w")
if len(line) == 1: # only the end-of-line character
continue;
outfile.write(line)
if line.startswith('$'):
outfile.close()
outnum += 1
continue;
if not outfile.closed: # last line didn't start with $
outfile.close()
My hope is that you will study this and see how the code weaves together the various aspects of looping over input and keeping the output file open to the current number. Some extensions you could do: have comment lines which start with #; use the split method of string to have comments that can start anywhere on the line; use the re module to remove lines that are all blanks; make the script into a function called when the file is executed; use the unittest module to test your changes.