Python invalid syntax with “with” statement

后端 未结 1 663
小鲜肉
小鲜肉 2021-02-12 23:50

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:33

    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)
提交回复
热议问题