這一系列博客僅用於記錄自己在學習python過程中的一些知識點。
將數組內容寫入文件。
代碼如下:
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
# Python Version 2.7.5
# 函數用戶: 將數組內容寫入文件
# 參數:
# FILENAME:非空字符串。文件全路徑。例如:/var/log/a.txt
# LINES_ARR:非空數組。數組中的內容是按行分割的文件內容。
# 返回值:
# 無
def WriteFileFromArray(FILENAME, LINES_ARR):
FILE_NEW = "".join(LINES_ARR)
FILE_TMP = open(FILENAME, "w")
FILE_TMP.write(FILE_NEW)
FILE_TMP.close()
return True
if __name__ == '__main__':
FILEA = "/root/filea.txt"
FILEA_CONTENT_ARR = ['The first line is hello world\n', 'The second line is hellohello world\n', 'The third line is hellohellohello world\n']
WriteFileFromArray(FILEA, FILEA_CONTENT_ARR)
執行結果:
[root@LvZS-CentOS7 ~]# python WriteFileFromArray.py
[root@LvZS-CentOS7 ~]# cat filea.txt
The first line is hello world
The second line is hellohello world
The third line is hellohellohello world