Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Write an empty line at the end of my files

kkhhhkk

Coder
I have the following code that writes out multiple files. I want this code to also write out an empty line at the end of each file produced, I've tried adding ('\n\') inside the loop however it adds an empty line to every line which is annoying

Python:
i = 0
f = open('innput.txt','r')
always_print = False
with f as fp:
    lines = fp.readlines()
    for line in lines:
       if ' X ' in line:
           i += 1
           ff = open('dipole_'+str(i)+'.com','a')
           ff.write('Header')
           always_print = True
       elif always_print:
           ff.write(line)
            ff.write('\n\n')
       else:
           pass
       if always_print:
          continue
 
Solution
D
This is a common situation and I don't think there is a really elegant solution for it. You'll need to remember what file you are currently writing to (if any). Each time you hit the 'X', write a new line to that file (if any) and close it. Then open the new file and write the header to it. At the end of your loop, write the newline to the current file and close it. Closing your files would be a good thing to do anyway, I'm not at all sure if Python will do that if you open a new file with the same ff so you may well end up with any number of open files and rely on Python to close them all when done.

Also, I'm not sure what your always_print variable is supposed to achieve. That last conditional continue is bogus...
You might could do something like

Python:
some_file = 'Python/my.txt'

with open(some_file, 'r') as file, open('another.txt', 'a') as another_file:
    lines = map(str.rstrip, file)
    for line in lines:
        if 'third' in line:
            another_file.write(f'{line}\n')
    another_file.write("\n")
 
This is a common situation and I don't think there is a really elegant solution for it. You'll need to remember what file you are currently writing to (if any). Each time you hit the 'X', write a new line to that file (if any) and close it. Then open the new file and write the header to it. At the end of your loop, write the newline to the current file and close it. Closing your files would be a good thing to do anyway, I'm not at all sure if Python will do that if you open a new file with the same ff so you may well end up with any number of open files and rely on Python to close them all when done.

Also, I'm not sure what your always_print variable is supposed to achieve. That last conditional continue is bogus since the last line of your loop code has no other option but doing just that. And anyway you don't need a boolean as you have the loop counter to tst on.

I'd use logic like this :
Code:
i = 0
for each input line

    if line has X
        if i > 0
            write LF to file i
            close file i
        i = i+1
        open file i
        write header to file i
       
     if i > 0       
         write line fo file i

if i > 0
    write LF to file i
    close file i

Note, this is pseudocode, I haven't tested it but I think it should do the job.
 
Solution
The always_print and continue is there since the occurrence happens over 1300 times within a large text file, if I remove this from the code it doesn't write everything I want.
No matter how large the input it, this "solution" puzzles me. Consider the last statement in your loop:

Python:
     if always_print:
          continue

If at this moment always_print is true, what happens ? It continues the loop (insofar as we are not at the end), right ?
If at this moment always_print is false, what happens then ? It continues the loop (insofar as we are not at the end) ! Because it's the last statement in the loop and there is nowhere else to go.
So, unless I am missing something bleedingly obvious, that statement can be taken out as it makes no difference. And then is follows that always_true is never used and can be taken out.
If you say that without this code it does not print everything you want, what exactly does that mean ? And why do you believe the size of the file to be a factor at all ?

EDIT - I take back the underlined statement above. I had not looked properly, sorry. Now it seems possible that the trick with always_print is indeed necessary. If it all works correctly now, as I think you suggested, let's consider it solved and move on.
 
Last edited by a moderator:
First of all I apologize for the typos in the previous post. Perhaps you could tell me what you actually want the code to do. My interpretation is:

You have a set of n files

dipole_1.com
dipole_2.com
.
.
.
dipole_n.com

and a file, innput.txt. You want to read innput.txt and every time you find a line containing 'X' you want to

select the next dipole_i.com file
append the line 'Header' + the line containing 'X' + '\n'

Is that correct? If not then perhaps you could post some sample inputs and outputs to illustrate.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom