Python學習知識整理(3)-將數組內容寫入文件

這一系列博客僅用於記錄自己在學習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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章