Add new line to a file after matching pattern - Python - python-3.x

I have a file called 'file.txt' and its contents are as below.
[Jack]
sv0f3fj3jff0j
[Tom]
343767y6y6y5yu
I have to add new line just after each names(by reading the user name as input). Can any one please help me ? I have tried using the below steps but didn't succeeded.
#!/usr/bin/python36
inv_file = '/root/file.txt'
cn_search = input("\nEnter the name: ")
new_line = input("\nEnter the new line : ")
with open(inv_file) as in_file:
buf = in_file.readlines()
print(buf.replace('[').replace(']'))
with open(inv_file, "w") as in_file:
for line in buf:
if line.startswith('[') and line.endswith(']'):
mod_line = line.replace('[', '').replace(']', '')
if mod_line == cn_search:
buf = buf + "\n" + new_ip_ex
out_file.write(buf)

It is correct to read and then rewrite the file, but you're also changing the brackets which is unnecessary. Do it like this:
import re
with open(inv_file) as in_file:
old_contents = in_file.readlines()
with open(inv_file, 'w') as in_file:
for line in old_contents:
in_file.write(line)
if re.search(r'\[.*\]', line):
in_file.write('YOUR MESSAGE HERE\n')

Related

fetch a string in file & get all lines containing the string along with line numbers

Also is it the correct code which works to fetch a string in file & get all lines containing the string along with line numbers
am getting syntax error in line 1 for the code,
def matched_lines('sam.txt', string_to_search):
matched_lines = search_string_in_file('sam.txt','is')
"""Search for the given string in file and return lines containing that string,
along with line numbers"""
line_number = 0
list_of_results = []
# Open the file in read only mode
with open('sam.txt', 'r') as matched_lines:
print('Total Matched lines : ', len(matched_lines))
# Read all lines in the file one by one
for elem in matched_lines:
print('Line Number = ', elem[0], ' :: Line = ', elem[1])
please help me.
Is this result needed?
def matched_lines(filename, string_to_search):
list_of_results = []
with open(filename, encoding='utf8')as matched_lines:
for elem in enumerate(matched_lines.read().split('\n')):
if string_to_search in elem[1]:
list_of_results.append(elem)
return list_of_results
result = matched_lines('some.txt', 'lorem')
print('Total Matched lines :', len(result))
print(result)

How to find and replace string in a file using input of line number in python

My requirement is to find a file from a directory and then in that file find LOG_X_PARAMS and in that append a string after the first comma this is what i am having for now
import os, fnmatch
def findReplacelist(directory, finds, new_string, file):
line_number = 0
list_of_results = []
for path, dirs, files in os.walk(os.path.abspath(directory)):
if file in files:
filepath = os.path.join(path, file)
with open(filepath, 'r') as f:
for line in f:
line_number += 1
if finds in line:
list_of_results.append((line_number))
print(list_of_results)
def get_git_root(path):
Path = "E:\Code\modules"
file_list=["pb_sa_ch.c"]
for i in file_list:
findReplacelist(Path , "LOG_1_PARAMS", "instance", i)
The example line is below change
LOG_X_PARAMS(string 1, string 2); #string1 andd string2 is random
this to
LOG_X_PARAMS(string 1, new_string, string 2);
I can find the line number using LOG_X_PARAMS now using this line number I need to append a string in the same line can someone help solving it ?
This is how I would do the task. I would find the files I want to change, read then file line by line and if there is a change in the file, then write the file back out. Heres the approach:
def findReplacelist(directory, finds, new_string, file):
for path, dirs, files in os.walk(os.path.abspath(directory)):
if file in files:
filepath = os.path.join(path, file)
find_replace(finds, new_string, filepath)
def find_replace(tgt_phrase, new_string, file):
outfile = ''
chgflg = False
with open(file, 'r') as f:
for line in f:
if tgt_phrase in line:
outfile += line + new_string
chgflg = True
else:
outfile += line
if chgflg:
with open(file, 'w') as f:
f.write(outfile)

Comparison script diffrent files

i am trying to write a script that compares a bunch of files based on a search word, in this case i searched for 106, then i want the code to match the words from file 1 to the words in file 2 and print a list with the ones that dont match.
For example in file A i have this line
106_LB01_GP61_HAL;LB01;10892;DIGITAL;0;0;0;0;;;Smutsigt tilluftsfilter;;
and in file B i have
"Prefix": "106_LB01_GP61",
those lines match and then i want it to ignore that tag
when the script find lines that dont match etc when a tag in file A cant fint its buddy in file B i want it to write those tags to a file,
for example:
Total unused tags:1
106_LB01_GP61
right now i am stuc at making it read to diffrent files at the same time
#!/usr/bin/env python
#Import os module
import os
# Ask the user to enter string to search
search_path = (".")
file_type = (".wpp")
search_str = input("Enter searchword: ")
resultsFile = "results.csv"
file_name = ("results.csv")
# Append a directory separator if not already present
if not (search_path.endswith("/") or search_path.endswith("\\") ):
search_path = search_path + "/"
# If path does not exist, set search path to current directory
if not os.path.exists(search_path):
search_path ="."
0
# Repeat for each file in the directory
for fname in os.listdir(path=search_path):
# Apply file type filter
if fname.endswith(file_type):
# Open file for reading
fo = open(search_path + fname)
# Read the first line from the file
line = fo.readline()
# Initialize counter for line number
line_no = 1
# Loop until EOF
wf = open(search_path + resultsFile, 'a')
while line != '' :
# Search for string in line
index = line.find(search_str)
if ( index != -1) :
print(fname, "[", line_no, ",", index, "] ", line, sep="")
wf.write(line + " ")
# Read next line
line = fo.readline()
# Increment line counter
line_no += 1
# Close the files
fo.close()
def check_if_string_in_file(file_name, string_to_search):
""" Check if any line in the file contains given string """
# Open the file in read only mode
with open(file_name, 'r') as read_obj:
# Read all lines in the file one by one
for line in read_obj:
# For each line, check if line contains the string
if string_to_search in line:
return True
return False
def check_if_string_in_file(file_name2, string_to_search):
""" Check if any line in the file contains given string """
# Open the file in read only mode
with open(file_name2, 'r') as read_obj:
# Read all lines in the file one by one
for line in read_obj:
# For each line, check if line contains the string
if string_to_search in line:
return True
return False
def search_string_in_file(file_name, string_to_search):
"""Search for the given string in file and return lines containing that string,
along with line numbers"""
line_number = 0
list_of_results = []
# Open the file in read only mode
with open(file_name, 'r') as read_obj:
# Read all lines in the file one by one
for line in read_obj:
# For each line, check if line contains the string
line_number += 1
if string_to_search in line:
# If yes, then add the line number & line as a tuple in the list
list_of_results.append((line_number, line.rstrip()))
# Return list of tuples containing line numbers and lines where string is found
return list_of_results
def search_multiple_strings_in_file(file_name, list_of_strings):
"""Get line from the file along with line numbers, which contains any string from the list"""
line_number = 0
list_of_results = []
# Open the file in read only mode
with open(file_name, 'r') as read_obj:
# Read all lines in the file one by one
for line in read_obj:
line_number += 1
# For each line, check if line contains any string from the list of strings
for string_to_search in list_of_strings:
if string_to_search in line:
# If any string is found in line, then append that line along with line number in list
list_of_results.append((string_to_search, line_number, line.rstrip()))
# Return list of tuples containing matched string, line numbers and lines where string is found
return list_of_results
def main():
print('*** Loading *** ')
matched_lines = search_string_in_file(file_name, search_str)
for elem in matched_lines:
print('Line Number = ', elem[0], ' :: Line = ', elem[1])
# search for given strings in the file 'sample.txt'
matched_lines = search_multiple_strings_in_file(file_name, [search_str])
print('*** Checking if', [search_str], 'exists in a file *** ')
print('Total Matched lines : ', len(matched_lines))
# Check if string 'is' is found in file 'sample.txt'
if check_if_string_in_file(file_name, search_str):
print('Yes, string found in file')
else:
print('String not found in file')
if __name__ == '__main__':
main()

Printing Out a Word Starting with a Particular String

I could use some help with my below code. I am trying to read through the file mbox-short.txt line by line. Looking at each word in the line to see if it starts with the string "SAK-", if so print it out and continue. Right now my code is finding all the lines with SAK, but instead of just printing out the SAK- string it's showing everything else following the string as well.
fname = input('Enter File: ')
if len(fname) < 1 : fname = 'mbox-short.txt'
lst = list()
fhand = open(fname)
for line in fhand:
line = line.strip()
if not line.startswith('SAK'): continue
words = line.split()
print(words)

Python-2.7 write to file

I have this script:
f = open("/ggg/darr/file/", "r+")
a = 0
for line in f:
if a ==58:
print (line)
line1 = "google.ca"
f.write(line1)
print line
a = a+1
f.close()
I want to keep my file but only to change what is written on line 58 to "google.ca"
then save it
using linux: mint-17.2
# Read data from file
with open('yourfile.txt', 'r') as file:
# read all line in the file to data array
data = file.readlines()
# change data on line 58 (array start from 0)
data[57] = 'Data you need to change'
# Write data back
with open('yourfile.txt', 'w') as file:
file.writelines(data)
You need to decide whether you want to write a new file (with print) or change the old file (with r+ mode and f.write). You will probably be happiest if you write a new file.
dataRead = []
f = open("/ggg/darr/file/", "r+")
a = 0
for line in f:
if a == 58:
line = "google.ca"
dataRead.append(line)
a = a+1
f.close()
f2 = open("/some/new/file","w")
for line in dataRead:
f2.write(line)
f2.close()
With the answer of Adisak Anusornsrirung I wrote it like this:
with open('sss.txt','r') as file:
data = file.readlines()
print (data[14])
file.close()
data[14] = "some data here"+"\n"
with open ("sss.txt", 'w') as file:
file.writelines(data)
file.close()
f = open("sss.txt", 'r')
print (f.read())
f.close()

Resources