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 9025037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:10:23+00:00 2026-06-16T06:10:23+00:00

I would like to create a python script that will do 3 things: 1)

  • 0

I would like to create a python script that will do 3 things:

  • 1) Take user input to navigate to a file directory
  • 2) Confirm the file contents (a particular set of files need to be in the folder for the script to proceed)
  • 3) Do a Find and Replace
  • The Code as of now:

    import os, time
    from os.path import walk
    
    mydictionary = {"</i>":"</em>"}
    
    for (path, dirs, files) in os.walk(raw_input('Copy and Paste Course Directory Here: ')):
        for f in files:
            if f.endswith('.html'):
                filepath = os.path.join(path,f)
                s = open(filepath).read()
                for k, v in mydictionary.iteritems(): terms for a dictionary file and replace
                    s = s.replace(k, v)
                f = open(filepath, 'w')
                f.write(s)
                f.close()
    

    Now i Have parts 1 and 3, I just need part 2.

    for part 2 though I need to confirm that only html files exist in the directory the the user will specified otherwise the script will prompt the user to enter the correct folder directory (which will contain html files)

    Thanks

    • 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-06-16T06:10:24+00:00Added an answer on June 16, 2026 at 6:10 am

      From what I understand, here’s your pseudocode:

      Ask user for directory
      If all files in that directory are .html files:
          Do the search-and-replace stuff on the files
      Else:
          Warn and repeat from start
      

      I don’t think you actually want a recursive walk here, so first I’ll write that with a flat listing:

      while True:
          dir = raw_input('Copy and Paste Course Directory Here: ')
          files = os.listdir(dir)
          if all(file.endswith('.html') for file in files):
              # do the search and replace stuff
              break
          else:
              print 'Sorry, there are non-HTML files here. Try again.'
      

      Except for having the translate the “repeat from start” into a while True loop with a break, this is almost a word-for-word translation from the English pseudocode.

      If you do need the recursive walk through subdirectories, you probably don’t want to write the all as a one-liner. It’s not that hard to write “all members of the third member of any member of the os.walk result end with '.html'“, but it will be hard to read. But if you turn that English description into something more understandable, you should be able to see how to turn it directly into code.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
        • Report

    Sidebar

    Related Questions

    I have a python script that will take each file in a directory, iterate
    I would like to run a script that will create a simple HTTP server
    I would like to create a script, that will play for me a facebook
    I would like to write a python script that would upload any file I
    I would like to create a class that behaves as a python variable but
    I need to create a python module that will be installed on end-user machines.
    I'm trying to create a script that will upload a file to a website.
    I am trying to create a simple python script that deploys my EAR file
    I am trying to create a script in Python that will collect data put
    I am trying to create a Python script which will take an address as

    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.