python 写错误

标签 python join for-loop 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)

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

最佳答案

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

关于 python 写错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15583236/

相关文章:

javascript - 如何更改 for-of 循​​环中的元素

创建包含其他对象的对象列表时出现的 Python 基本问题

sql - 能否在 Postgres 中有效地 LEFT OUTER JOIN 左表行的子集?

mysql - 连接三个表 MySQLWhere

MYSQL 从同一列中选择多个值作为一行

Python Seaborn堆叠条形图多列

android - 是否可以编写一个 for 循环来将监听器分配给许多具有相同功能但使用最终整数的按钮?

python - 网页抓取到 .csv

python - 从项目列表创建特定形状的 Pandas 数据框

python - 基本反向传播实现不起作用