Python Forum
What mode should i open the file to write into a *.msh file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What mode should i open the file to write into a *.msh file
#1
Hi,
I am trying to update a *.msh file by finding a 'find_word' and replace it with rep_word'.
I wrote a program to read an input csv file where I will be giving these fields. - file_type(values input are msh,mshs), folder_path(path where files are to be searched for the word and replaced), sub_directory(ignore as of now), find_word(the workd which needs to be searched), rep_word(the word to be replaced).
import os
import re
import csv

"""Replace a word in the file with the replace word and writes into the file"""
def check_and_replace(file, find_word, rep_word):
        rfile = open(file, 'rt')
        string = rfile.read()
        rfile.close()
        string = re.sub(find_word, rep_word, string)
        wfile = open(file, 'wt')
        wfile.write(string)
        wfile.close()

"""Reads the input test file and gets the values for file type, folderpath, subdirectory value, find word and replace word"""
"""file_type will have values like msh or mshs"""
"""folder_path will have value of the folder where replace is done"""
"""find word is the search word and rep_word is the word to be replaced"""
with open('test csv.csv', 'r') as csv_file:
    next(csv_file)
    csv_reader = csv.reader(csv_file)
    for line in csv_reader:
        file_type = line[0]
        folder_path = line[1]
        sub_directory = line[2]
        find_word = line[3]
        rep_word = line[4]
        for path, subdirs, files in os.walk(folder_path):
            for file in files:
                if file.endswith(file_type):
                    new_file_name = os.path.join(path, file)
                    check_and_replace(new_file_name, find_word, rep_word)
sample msh file
input file
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 315 Jan-24-2024, 06:28 PM
Last Post: frohr
  Last record in file doesn't write to newline gonksoup 3 404 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  file open "file not found error" shanoger 8 1,087 Dec-14-2023, 08:03 AM
Last Post: shanoger
  write to csv file problem jacksfrustration 11 1,503 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,429 Nov-09-2023, 10:56 AM
Last Post: mg24
  Need to replace a string with a file (HTML file) tester_V 1 761 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can i combine these two functions so i only open the file once? cubangt 4 852 Aug-14-2023, 05:04 PM
Last Post: snippsat
  How can I change the uuid name of a file to his original file? MaddoxMB 2 921 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  How do I read and write a binary file in Python? blackears 6 6,507 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,089 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020