参考:https://www.jb51.net/article/137644.htm
f = open('E:/test.txt','w')
f.write('hello world!')
f.close()
f = open('E:/test.txt','a')
f.write('the second writing...') #会接着写同一行,不会换行的
f.close()
f = open('E:/test.txt','a')
f.write('\nthe third writing...') #如果要换行,需要使用\n
f.close()
f = open('E:/test.txt','a')
f.writelines(['\nthe fourth writing...',',','good']) #将多个字符串写到同一行
f.close()
本文详细介绍了如何使用Python进行文件操作,包括写入、追加、换行及多字符串写入等常见操作。通过实例演示了不同模式下文件的写入方式,如直接追加、换行追加以及同时写入多个字符串。
2万+

被折叠的 条评论
为什么被折叠?



