Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6016335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:58:04+00:00 2026-05-23T02:58:04+00:00

I have the current script included below that goes into a file with extension

  • 0

I have the current script included below that goes into a file with extension .las and replaces certain strings with others (ie.: cat -> kitten, dog -> puppy).

All I want is to add in a functionality into this script that would rename ANY .las file to a certain name in the current directory when I run the script (ie.: *.las -> animals.las).

I would drag a single file into this directory, run the script, which performs the text replacement and the rename and then move the file out of the current directory. So for this script, I don’t care that it would rewrite multiple .las files to a single name.

# read a text file, replace multiple words specified in a dictionary
# write the modified text back to a file

import re
import os
import time

# the dictionary has target_word:replacement_word pairs
word_dic = {
'cat' : 'kitten',
'dog' : 'puppy'
}


def replace_words(text, word_dic):
    """
    take a text and replace words that match a key in a dictionary with
    the associated value, return the changed text
    """
    rc = re.compile('|'.join(map(re.escape, word_dic)))
    def translate(match):
        return word_dic[match.group(0)]
    return rc.sub(translate, text)

def scanFiles(dir): 
    for root, dirs, files in os.walk(dir):
        for file in files:
            if '.las' in file:
            # read the file
                fin = open(file, "r")
                str2 = fin.read()
                fin.close()
            # call the function and get the changed text
                str3 = replace_words(str2, word_dic)
            # write changed text back out
                fout = open(file, "w")
                fout.write(str3)
                fout.close()
                #time.sleep(1)



scanFiles('')

I pasted the script together from online examples so I don’t know all the inner workings of it, so if anyone has a more elegant/efficient way of doing what this script is doing, I am open to changing it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:58:04+00:00Added an answer on May 23, 2026 at 2:58 am

    If you want to end up with a single file named animals.las containing the contents of *.las, then you can change the scanFiles function to open animals.las at the start of the loop, write the translated output of each *.las file to animals.las, and then close animals.las:

    def scanFiles(dir): 
        fout = open("animals.las", "w")
        for root, dirs, files in os.walk(dir):
            for file in files:
                if '.las' in file:
                # read the file
                    fin = open(file, "r")
                    str2 = fin.read()
                    fin.close()
                # call the function and get the changed text
                    str3 = replace_words(str2, word_dic)
                # write changed text back out
                    fout.write(str3)
                    #time.sleep(1)
        fout.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bash script that creates a Subversion patch file for the current
I have a shell script which copies a few files to the current directory,
I have the current basic structure for each domain object that I need to
My current problem is that I have a JFrame with a 2x2 GridLayout. And
I have a script that appends some rows to a table. One of the
Alrighty so below I have this script doing exactly what I want; I'm using
We have a current application where user login credentials are stored in a SQL
I have a current system which is build as a Windows Application, and does
I have four flags Current = 0x1 Past = 0x2 Future = 0x4 All
I use bzr for websites. I have all my current ones set up so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.