What is StringIO in python used for in reality?

前端 未结 8 1524
囚心锁ツ
囚心锁ツ 2021-01-29 20:46

I am not a pro and I have been scratching my head over understanding what exactly StringIO is used for. I have been looking around the internet for some examples. However, almos

8条回答
  •  隐瞒了意图╮
    2021-01-29 21:11

    Django has a function call_command which is used to call management commands. This function prints output to stdout and doesn't return any value. If you want to know whether the command ran successfully or not, you have to look into output and decide.

    Using StringIO, you can capture output and check if it is desired output or not.

    with io.StringIO() as output:
        call_command('custom_command', stdout=output)
        if 'Success' not in output.getvalue():
            print('Custom command failed...')
    

提交回复
热议问题