0

I'm trying to write a program which can run on my windows laptop to track its connectivity but one of the python packages I want to use needs to be run in a linux environment. That, itself, is not an issue though since windows now has official support for CLIs for OSe like Ubuntu; the issue lies in the fact that whenever I try to use this line of code

print(ThisIsAVariableToHoldMyString, end=" ")

it just doesn't print. I know the line is being called through repeated tests of my method (I deleted some commented out others but needless to say I have done a lot of tests)

 def AddToReturn(self, toadd, end = False):
  #print(toadd)
  #print(end)
  if end:
   print("last hash : "+toadd)
   self.returnstring += self.returnstring + "last hash : "+toadd
   return self.returnstring
  else:
   print(toadd, end="")
   self.returnstring += toadd

and they always printed whatever I asked them to. Even the first non-commented print statement print("last hash : "+toadd) works fine and I can see its output, the only one that isn't working is the one with end="" in it. Is this just me being an idiot or is it an actual issue with the windows ubuntu environment? I already tried flush=True.

  • The problem might not be in your Python code, but in something intermediate. Are there additional buffers involved between where you're looking for the output and where it's printed? Does it go through a pipe to some other program, or through some cache somewhere? – joanis Apr 12 '21 at 16:22
  • One different I found between Windows and Linux systems is that typically, on windows, a flush in your software flushes right through to the physical disk (or at least, it did in a context I faced about 10 years ago - my info could be dated!), whereas in Linux it flushes it out of your program but it might still be in the OS's buffers. So, you might need to provide a bit more context about how you call this program on the Ubuntu side. – joanis Apr 12 '21 at 16:24
  • Voting to re-open: this problem appears to be about more than just flushing within Python. – joanis Apr 12 '21 at 16:26
  • PS: if the only print statement you don't see is the one with `end=""`, I wonder if you are piping it into a line-buffered downstream process which is itself waiting on the newline before showing the line. – joanis Apr 12 '21 at 16:28
  • I just call it by opening an ubuntu tab in windows terminal preview and running it there. As for how the method itself is called it is mostly called from the class it is within, with one exception from another class' method which finishes it. This is roughly how the program works: 1st : a loop is launched from within a class (handles some logging, hashing, etc) 2nd : that loop calls a method from another class which handles MOST of the actual data generation. 3rd : that loop gets its data, but stores it using the method listed in my post 4th : the first class calls the method to end it – TenDaysTillDallas Apr 12 '21 at 16:29
  • Darn, that means I have no obvious solution to propose. In an actual terminal windows, flushing to screen should happen with `flush=True`. I suggest you edit your question to show the version of the code that still doesn't work despite using `flush=True`, so other reviewers vote to reopen it. Hopefully someone with a configuration similar to yours might know the answer. – joanis Apr 12 '21 at 16:33
  • Dumb question: if you change `print(toadd, end="")` by `print(toadd)`, then it does go to screen as expected, right? I'm not following your full calling logic, but this test would eliminate this thing not being called when you expect it to be... – joanis Apr 12 '21 at 16:39

0 Answers0