1

I am trying to create a simple file editor in Python via the console (called PyCons). Here is my current program:

def PyCons(f):
  file = open(f, "r")
  appe = open(f, "a")
  print("\n=====\nPyCons Editor, v1.2\nPython 3.6.1\n")
  global i
  i = 0
  for line in file:
    i += 1
    print(" {}|  ".format(i) + line, end='')
  for k in range(10000000000000):
    print("\n {}| ".format(i+1), end='')
    inp = input("")
    if inp == "@PyCons.save()":
      print("\n=====")
      break
    else:
      i += 1
      appe.write("\n" + inp)

I used the end method to make sure that the existing code in the file was printed properly in its line format, but the input function does not have an end attribute. So, when I go to enter code in the editor:

PyCons("magic.html")    
...

=====
PyCons Editor, v1.2
Python 3.6.1

 1|  <p>Hello!</p>
 2|  <h1>Big text!</h1>
 3|  <h2>Smaller text!</h2>
#Should be no spaces here
 4|  <p>More stuff!</p>
#No spaces here either
 5|  @PyCons.save()

=====

...I get those big, nasty spaces between my inputs. Does anybody know a way to suppress the output of this space, similar to the end='' method used for the print function?

EDIT: Project location for reference: https://repl.it/@xMikee/File-Editing

halfer
  • 19,824
  • 17
  • 99
  • 186
miike3459
  • 1,431
  • 2
  • 16
  • 32

1 Answers1

2

The newlines from the inputs are just local echos from the console. You see them on your screen but they aren't actually returned by the input function.

The real problem is with the newline you explicitly print before every line number. Remove the \n so the line that prints line numbers becomes:

print(" {}| ".format(i+1), end='')

Furthermore, the file you're loading may not necessarily have a trailing newline in the end, so you need to detect that and print a newline if that's the case. Note what I added after your first for loop:

def PyCons(f):
    file = open(f, "r")
    appe = open(f, "a")
    print("\n=====\nPyCons Editor, v1.2\nPython 3.6.1\n")
    global i
    i = 0
    for line in file:
        i += 1
        print(" {}| ".format(i) + line, end='')
    if not line.endswith('\n'):
        print()
    for k in range(10000000000000):
        print(" {}| ".format(i+1), end='')
        inp = input("")
        if inp == "@PyCons.save()":
            print("\n=====")
            break
        else:
            i += 1
            appe.write("\n" + inp)
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • That does not work because of my usage of `end=''` (it makes it so the new input is on the same line as the previous one). – miike3459 Jun 30 '18 at 18:19
  • That actually works. I tried your code with this change myself. Did you try it? – blhsing Jun 30 '18 at 18:20
  • Maybe insert a `print("")` between for loops, then remove the newline? – Dillon Davis Jun 30 '18 at 18:20
  • @connectyourcharger Well, why did you print a `\n` before every line number in the first place anyway? – blhsing Jun 30 '18 at 18:24
  • @blhsing Because if I don't, this happens: `4|

    More stuff!

    5|` because I put the print's `end` to `''`
    – miike3459 Jun 30 '18 at 18:26
  • @connectyourcharger But your keyboard input gives you the newlines locally when you type and hit Enter, so you don't need to print newlines from the code. – blhsing Jun 30 '18 at 18:30
  • @blhsing Using `end=''` on the print statement technically puts the start of the "new line" at the end of the old one. – miike3459 Jun 30 '18 at 18:32
  • @connectyourcharger The point is you are letting the user type a newline as input after displaying a line number as a prompt, so when the user sees ` 3| ` as a prompt and types `

    Smaller text!

    ` and then hits *Enter*, the Enter key he hits will bring the cursor to a new line because of local echo.
    – blhsing Jun 30 '18 at 18:35
  • @blhsing Could you provide a link to your edited program that works? – miike3459 Jun 30 '18 at 18:38
  • @connectyourcharger My edited program is nothing more than removing `\n` from your `print("\n {}| ".format(i+1), end='')` line. But now I realize what your problem might be. The file you load may not have a trailing newline in the end, so you need to print a new line yourself if that's the case – blhsing Jun 30 '18 at 18:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174091/discussion-between-connectyourcharger-and-blhsing). – miike3459 Jun 30 '18 at 18:44
  • 1
    @connectyourcharger I just edited my answer with the suggestion I just made to account for the lack of a trailing new line in the file you're loading. Give it a try then. – blhsing Jun 30 '18 at 18:44