3

I'm trying to write the psutil.test() result to a file but instead it prints out the text that I want in the file and writes "None" to the test.txt.

import psutil
from time import sleep
while True:
    proccesses = psutil.test()
    file = open("test.txt", "a")
    file.write(str(proccesses))
    file.write("\n" * 10)
    file.close()
    sleep(5)
MSeifert
  • 145,886
  • 38
  • 333
  • 352
Lojas
  • 195
  • 3
  • 13
  • 1
    I'm guessing `psutil.test()` is producing output to `stdout` or the like, not intended to return output to the user. A function that doesn't have an explicit return value returns `None`. Check the docs for the function to see how it's supposed t owork. – ShadowRanger Oct 13 '17 at 01:03

2 Answers2

2

psutil.test() doesn't return a string. It prints a string. A workaround would be to use contextlib.redirect_stdout, so that string goes to your file instead of STDOUT.

import psutil
from contextlib import redirect_stdout
from time import sleep

with open("test.txt", "a") as file:
    with redirect_stdout(file):
        while True:
            psutil.test()  # Will print to file.
            file.write("\n" * 10)  # or print("\n" * 10)
            sleep(5)

Be sure to use both context managers (the with statements), or your file won't be flushed and closed. The redirect_stdout documentation uses separate context managers for the file and the redirection.

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
2

psutil.test() just prints to stdout but returns None.

You could use contextlib.redirect_stdout to redirect the standard output (e.g. when using print) to a file:

import contextlib
import time
import psutil

while True:
    with open("test.txt", 'a') as fout, contextlib.redirect_stdout(fout):
        psutil.test()
        print("\n" * 10)
    time.sleep(5)
MSeifert
  • 145,886
  • 38
  • 333
  • 352
  • Does this close the file? – Arya McCarthy Oct 13 '17 at 01:04
  • @AryaMcCarthy Yes, upon leaving the context manager it flushes and closes the file :) – MSeifert Oct 13 '17 at 01:06
  • I'm not so sure, since the file handle returned by `open()` isn't treated as a context manager itself. Also, it should be opened with mode `'a'` for append. – Arya McCarthy Oct 13 '17 at 01:07
  • @AryaMcCarthy I'm pretty sure it closes the file but you're right putting it in a contextmanager isn't hard. – MSeifert Oct 13 '17 at 01:12
  • The [`redirect_stdout` documentation](https://docs.python.org/3/library/contextlib.html#contextlib.redirect_stdout) uses separate context managers for the file and the redirection. – Arya McCarthy Oct 13 '17 at 01:17
  • @AryaMcCarthy I'm also using several contextmanagers. Just seperated with `,` instead of nested. – MSeifert Oct 13 '17 at 01:19
  • @AryaMcCarthy Yes, even though CPython deletes and thereby closes the IO-object when it's not referenced anymore (like when the contextmanager exits and no variable references it anymore). Other implementations (PyPy, etc.) might not... – MSeifert Oct 13 '17 at 01:26