文件写入(两种)

第一种:如下所示参数'w',往文件中写入类容,若是filePath不存在,则会创建新的文件

  需要注意的是,使用‘w’会将以前文件中存在的数据全部清除掉


1
fileOpen = open('filePath','w') 2 fileOpen.write('string') 3 fileOpen.close()

第二种:如下使用参数‘a’,往文件中追加类容,若是文件不存在,会创建新的文件

  使用‘a’以前文件中存在的数据不会被清除,只会在后面追加

1 fileOpen = open('filePath','a')
2 fileOpen.write('string')
3 fileOpen.close()
原文地址:https://www.cnblogs.com/tanghuang/p/6202799.html