In Java, how can I redirect System.out to null then back to stdout again?

Man, this is not so good, because Java is cross-platform and ‘/dev/null’ is Unix specific (apparently there is an alternative on Windows, read the comments). So your best option is to create a custom OutputStream to disable output. try { System.out.println(“this should go to stdout”); PrintStream original = System.out; System.setOut(new PrintStream(new OutputStream() { public void … Read more

“subprocess.Popen” – checking for success and errors

Do you need to do anything with the output of the process? The check_call method might be useful here. See the python docs here: https://docs.python.org/2/library/subprocess.html#subprocess.check_call You can then use this as follows: try: subprocess.check_call(command) except subprocess.CalledProcessError: # There was an error – command exited with non-zero code However, this relies on command returning an exit … Read more

How to make python 3 print() utf8

Clarification: TestText = “Test – āĀēĒčČ..šŠūŪžŽ” # this not UTF-8…it is a Unicode string in Python 3.X. TestText2 = TestText.encode(‘utf8′) # this is a UTF-8-encoded byte string. To send UTF-8 to stdout regardless of the console’s encoding, use the its buffer interface, which accepts bytes: import sys sys.stdout.buffer.write(TestText2)

How to test a function’s output (stdout/stderr) in unit tests

One thing to also remember, there’s nothing stopping you from writing functions to avoid the boilerplate. For example I have a command line app that uses log and I wrote this function: func captureOutput(f func()) string { var buf bytes.Buffer log.SetOutput(&buf) f() log.SetOutput(os.Stderr) return buf.String() } Then used it like this: output := captureOutput(func() { … Read more

Piping output of subprocess.Popen to files

You can pass stdout and stderr as parameters to Popen() subprocess.Popen(self, args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0) For example >>> import subprocess >>> with open(“stdout.txt”,”wb”) as out, open(“stderr.txt”,”wb”) as err: … subprocess.Popen(“ls”,stdout=out,stderr=err) … <subprocess.Popen object at 0xa3519ec> >>>