3

I've created a python script to output every 1-4 character letter combinations from AAAA-ZZZZ etc.

It works perfectly, however I require only a line feed to be inserted at the end of the printed variable, since I am using this as a word list to be used in another script.

I have attempted to use both \r and \n, however using \n inserts both a Carriage Return and a Line Feed at the end (e.g AAAA CR LF), using \r inserts only a Carriage Return (e.g AAAA CR).

My question is would it be possible to insert just a line feed at the end, or is this impossible due to a limitation in python? i've tried using notepad to manually replace each character, and this works, however this is just patching up the problem and is impractical for use.

Many thanks for any help!

import string
alpha = string.ascii_uppercase
w=(a+b+c+d for a in alpha for b in alpha for c in alpha for d in alpha) 
x=(a+b+c for a in alpha for b in alpha for c in alpha) 
y=(a+b for a in alpha for b in alpha) 
z=(a for a in alpha ) 
with open("Output.txt", "w") as text_file: 
for i in [w, x, y, z]:
    for k in i:
        print("{}\r".format(k), file=text_file, end='')

the problem here is the line

print("{}\r".format(k), file=text_file, end='')
TkrZ
  • 33
  • 1
  • 2
  • 5
  • 2
    Have you looked at this one? http://stackoverflow.com/questions/11018188/python-print-using-carriage-return-and-comma-not-working This may be a windows thing. – theWanderer4865 Mar 30 '17 at 14:48
  • Hey there, I did actually take a look at that question before posting this one, and couldn't find a solution, appending the flush argument to the end of the print command (flush=True) didn't seem to cause any difference. the \n should print only a line break, and not both a carriage return and line break, right? – TkrZ Mar 30 '17 at 14:59
  • if I figure this one out, I've made a mental note (and starred this) to come back and let you know. I was trying to learn how to work with bytes a few weeks ago so I might take a look at that again when I have some free time. I wonder why there is no dedicated escape sequence that represent STRICTLY LF rather than being platform dependent? That is what you mean right? – Twisted on STRIKE at1687989253 Nov 06 '21 at 23:23

3 Answers3

7

For files opened in text mode (the default), python interprets /n as equivalent to os.linesep, which on Windows is [CR][LF], and vice versa.

import os
print(os.linesep)

Check out http://docs.python.org/library/os.html

To prevent this, open the file in binary mode.

with open("Output.txt", "wb") as my_file: 

This tells Python to treat the file as a plain sequence of bytes and you will find that \n gets treated as a single linefeed character.

Community
  • 1
  • 1
Simon Hibbs
  • 5,941
  • 5
  • 26
  • 32
  • Hi there, I now get a "TypeError: a bytes-like object is required, not 'str' when writing to a file" which i'm assuming is something to do with the fact i'm using strings? – TkrZ Mar 30 '17 at 15:12
  • 1
    I hadn't noticed you're using Python 3, which means opening the file in binary mode reads it as a bytes object, not a string. That complicates things. Combining Python's implicit magical imposition of what platform format you want your text files to be and Python 3 bytes mode really hoses you I'm afraid. You'd need to write to it using text_file.write(). – Simon Hibbs Apr 03 '17 at 14:07
1

Try:

with open("Output.txt", "bw") as text_file: 
Claudio
  • 7,474
  • 3
  • 18
  • 48
1

In Python3 print() basically adds new line upon every execution. For this purpose I'd suggest you to use sys module's write function. I also had the same problem when I was making a download bar.

Code-

import sys
sys.stdout.write('\r')
sys.stdout.flush()

This should solve your problem.

  • Hi there, I implemented the sys.stdout.write to output to file, eradicating the use of print completely, however it still performs the same thing, either just a CR using \n , and both a CR and a LF with \r. No luck :( – TkrZ Mar 30 '17 at 15:26
  • Using \n Inserts both a Carriage Return and a Line Feed, where it should be inserting only a Line Feed with \n – TkrZ Mar 30 '17 at 15:37