📜  python代码替换txt文件的第一个值 - Python代码示例

📅  最后修改于: 2022-03-11 14:46:17.098000             🧑  作者: Mango

代码示例1
import os

for filename in os.listdir(os.getcwd()):
    if filename.endswith(".txt"):
            with open('{}' .format(filename), 'r+') as file:
                lines = file.readlines()
                file.seek(0, 0) #set the pointer to 0,0 cordinate of file
                for line in lines:
                    row = line.strip().split(" ")

                    if not int(row[0]):
                        row[0] = '1'
                        print(row)
                        file.write(" ".join(row) + "\n")