Python invalid syntax with “with” statement

后端 未结 1 1877
孤独总比滥情好
孤独总比滥情好 2021-02-13 00:18

I am working on writing a simple python application for linux (maemo). However I am getting SyntaxError: invalid syntax on line 23: with open(file,\'w\') as f

1条回答
  •  长发绾君心
    2021-02-13 00:48

    Most likely, you are using an earlier version of Python that doesn't support the with statement. Here's how to do the same thing without using with:

    fileh = open(file, 'w')
    try:
        # Do things with fileh here
    finally:
        fileh.close()
    

    0 讨论(0)
提交回复
热议问题