python學習-讀取txt文件內容並寫入另一個txt文件


讀取demo1文件的內容將其寫入到新的txt文件中

import random
import string
#生成當前時間戳
def random_name(name_len=4):
    seeds = string.digits
    random_str = []
    for i in range(name_len):
        random_str.append(random.choice(seeds))
    name = "".join((random_str))
    return name
#讀取文件內容,並寫入另一個文件
a_file = open("demo1.txt","r")
lua_li = ''
for lien in a_file.readlines():
    lien = lien.strip()
    lua_li = lua_li + str(lien) + random_name()
print(lua_li)

file = open("demo2.txt",'w')
file.write(lua_li)
file.close()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM