python写错误

Jos*_*123 1 python for-loop join overwrite

def save_friend(filename, friends_list):
    rows = ''
    strings = open(filename, 'Ur')
    for c in strings:
        rows += ','.join(c)
        rows += '\n'


    return strings.write(rows)
Run Code Online (Sandbox Code Playgroud)

尝试将列表覆盖(friends_list)到文件(filename).测试时不断收到此错误消息IOError: File not open for writing.但我以前的功能打开文件就好了..

A. *_*das 5

'w'如果要打开文件进行写入,则必须使用该模式.由于您已经使用'\n'作为换行符,因此您可以摆脱该'U'模式.