将数据写入python中的文件

cx6n0qe3  于 2022-11-28  发布在  Python
关注(0)|答案(1)|浏览(143)

我一直收到错误:FileNotFoundError:[Errno 2]没有这样的文件或目录:下面是我代码:

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

zzwlnbp8

zzwlnbp81#

请尝试以下操作:

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

相关问题