Your condition doesn't make any sense. The input
function will never return None
, only strings.
If you want to skip empty strings, a better test would be if not msg
(empty strings are "falsy"). Or, if you want to reject any all-whitespace strings, try if not msg.strip()
(which removes leading and trailing whitespace before checking if the rest of the string is empty or not).
Further, it's rarely a good idea to write an if
statement that just contains pass
. Instead, invert the test so that the condition is true for cases where you want to run some code (in this case, when msg
is not empty or all whitespace) and simply omit the cases where you'd do nothing:
while 1:
msg = input(">>:")
now = datetime.datetime.now()
now1 = str(now)
if msg.strip(): # msg is not empty or all whitespace
msg2 = now1+spc+msg+"\n"
if msg == "/close":
exit()
f.write(msg2)
f.flush()
One final issue (unrelated to the main question). Python's exit
function is primarily intended for use in the interactive interpreter. It is added to the builtins by the site
module, and so it won't exist if Python was run with the -S
flag. If you want to close the interpreter, you should instead call sys.exit
, raise a SystemExit
exception, or just run off the end of the main module (a break
statement would probably do that for the loop you've shown here, or perhaps a return
if you're in a function somewhere).