【问题标题】:Writing data to a file in python在python中将数据写入文件
【发布时间】:2022-11-27 05:46:55
【问题描述】:

我不断收到错误消息:FileNotFoundError: [Errno 2] No such file or directory: 'Bayofagbenro.txt' 这是我的代码:

def main():
    outfile = open('Bayofagbenro.txt')
    Bayofagbenro =outfile.write ('Modupeola\n')
    Bayofagbenro =outfile.w ('Ayobami\n')
    Bayofagbenro =outfile.w ('AKintola\n')
    Bayofagbenro =outfile.w ('Omonike\n')
    Bayofagbenro =outfile.w ('Fehintoluwa\n')
    Bayofagbenro =outfile.w ('Modupeola is 44yrs, Ayobami is 42 years, AKintola is 38 years, omonike is 36 years while fehintoluwa is 30 years')
    outfile = Bayofagbenro.r


    outfile.close


if __name__ == "__main__":
 main()

期待这样的事情:

print

【问题讨论】:

  • 尝试使用open('Bayofagbenro.txt', 'w') 你需要指定你想写给它

标签: python


【解决方案1】:

尝试这样的事情:

def main():
    with open('Bayofagbenro.txt', 'w') as f:
        f.write('Modupeola
')
        f.write('Ayobami
')
        f.write('AKintola
')
        f.write('Omonike
')
        f.write('Fehintoluwa
')
        f.write('Modupeola is 44yrs, Ayobami is 42 years, AKintola is 38 years, omonike is 36 years while fehintoluwa is 30 years')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 2015-01-27
    • 2017-03-29
    相关资源
    最近更新 更多