Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python code to replace first value of txt file

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) + "
")
Comment

PREVIOUS NEXT
Code Example
Python :: if statement in one-line for loop python 
Python :: create a timestamp python 
Python :: discord.py edit messages 
Python :: different states of a button tkinter 
Python :: pandas cumulative mean 
Python :: delay print in python 
Python :: run in thread decorator 
Python :: get name of month python 
Python :: python array input from user 
Python :: draw circle pygame 
Python :: change django administration text 
Python :: vim run python current file 
Python :: python fill zeros left 
Python :: python get zip file size 
Python :: python random number guessing game 
Python :: python datetime 
Python :: print inline output in python 
Python :: Sending POST request in Django 
Python :: python sum of list 
Python :: python edit string variable 
Python :: Extract bounding boxes OpenCV 
Python :: BeautifulSoup(raw_html 
Python :: converting numpy array to dataframe 
Python :: jupyter dark theme vampire 
Python :: check if numpy array contains only duplicates 
Python :: python how to show package version 
Python :: index from multiindex pandas 
Python :: replace list python 
Python :: df empty python 
Python :: reverse python 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =