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

  • SEARCH
  • Home
  • 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 9117401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:53:20+00:00 2026-06-17T04:53:20+00:00

I have the below code doing everything except what I want :-). The goal

  • 0

I have the below code doing everything except what I want :-). The goal is simple, I am trying to add the text to appendtext variable at the top of each file present in the directory (and subdirectories), the script runs fine but the text is not getting appended. Where am I going wrong?

import os
import sys
import fnmatch
temp_fname = "temp_file"

appendtext="""Test string
"""
if len(sys.argv) < 2:
    sys.exit('Usage: test.py <build directory>')

for path,dirs,files in os.walk(sys.argv[1]):
    for fname in files:
        for pat in ['*.*']:
            if fnmatch.fnmatch(fname,pat):
                fullname = os.path.join(path,fname)
                with open(fullname, "r") as in_file:
                    with open(temp_fname, "w") as out_file:
                        out_file.write(appendtext)
                        for line in in_file:
                            out_file.write(line)
                os.rename(temp_fname, fullname)
  • 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-17T04:53:21+00:00Added an answer on June 17, 2026 at 4:53 am

    If you want the text at the top of the file, you should do something like this:

    temp_fname = "temp_file"
    # the next line doesn't work in Python 2.5, 2.6, or 3.0
    with open(fullname, "r") as in_file, open(temp_fname, "w") as out_file:
        out_file.write(appendtext)
        for line in in_file:
            out_file.write(line)
    os.rename(temp_fname, fullname)
    

    Here is the above rewritten for Python 2.6:

    temp_fname = "temp_file"
    with open(temp_fname, "w") as out_file:
        with open(fullname, "r") as in_file:
            out_file.write(appendtext)
            for line in in_file:
                out_file.write(line)
    os.rename(temp_fname, fullname)
    

    We can do a little better than this. This always uses the same temporary filename ("temp_file") and that file will always be created in a single directory (the default directory when you run this). What we really want is a temporary file, with a unique name, created in exactly the same directory as the file we will be editing. Python provides us with a handy module called tempfile which creates temporary files.

    By default, you just get an open file handle and you don’t know the filename. But we need to know the filename, so that after the temp copy is fully done, we can rename it to the original file name. tempfile provides NamedTemporaryFile for cases like this.

    Here is a complete program:

    import fnmatch
    import os
    import sys
    import tempfile
    
    headertext = "# header text\n\n"
    
    def want_this_file(fname):
        for pat in ['*']:
            if fnmatch.fnmatch(fname, pat):
                return True
        return False
    
    def prepend_file(fullname, path):
        # with statement means temp file is written and closed at end of with
        with tempfile.NamedTemporaryFile(dir=path, delete=False) as out_file:
            with open(fullname, "r") as in_file:
                    out_file.write(headertext)
                    for line in in_file:
                        out_file.write(line)
            # before temp file is closed, get its name
            temp_fname = out_file.name
        # rename temp file to fullname, clobbering original
        os.rename(temp_fname, fullname)
    
    
    start_directory = sys.argv[1]
    
    for dirpath, dirnames, filenames in os.walk(start_directory):
        for fname in filenames:
            if want_this_file(fname):
                fullname = os.path.join(dirpath, fname)
                prepend_file(fullname, dirpath)
    

    This answer uses the pattern “write a temp file, then rename the temp file to the original name”. This is the way you should do it. It lets the code write the new version, and only when the new version is fully and successfully written, then does a single action to rename the new file to the old filename. Thus, if anything goes wrong while trying to write the new version, the original file is left untouched. This is a safe way to solve the problem.

    We want to create the temp file in the same directory as the original file, so that the os.rename() operation will be trivially cheap. On a Linux system, your system temp directory (/tmp) might be on its own partition, and if you just let tempfile create its temporary file there, then the rename operation might involve copying the data again! If the temp file is in the same directory, the rename operation is always very fast and safe.

    EDIT: Here is an improved version of the code. This catches errors, and cleans up the temp file before re-raising the exception signalling the error. Also, as J.F. Sebastian pointed out, the files should be opened in binary mode; this does that.

    import fnmatch
    import os
    import shutil
    import sys
    import tempfile
    
    file_patterns_to_match = ['*']
    
    headertext = "# header text\n\n"
    # make any newlines in headertext match the system line ending
    headertext = headertext.replace('\n', os.linesep)
    
    def want_this_file(fname):
        for pat in file_patterns_to_match:
            if fnmatch.fnmatch(fname, pat):
                return True
        return False
    
    def prepend_file(fullname, path):
        # with statement means temp file is written and closed at end of with
        with tempfile.NamedTemporaryFile(dir=path, delete=False) as out_file:
            # get the name immediately
            temp_fname = out_file.name
    
            try:
                # use binary mode to avoid newline translations
                with open(fullname, "rb") as in_file:
                    out_file.write(headertext)
                    shutil.copyfileobj(in_file, out_file)
            except Exception:
                # on any error, clean up temp file and re-raise exception
                try:
                    os.remove(temp_fname)
                except Exception:
                    print("unable to clean up temp file: " + temp_fname)
                    pass
                raise
        # rename temp file to fullname, clobbering original
        os.rename(temp_fname, fullname)
    
    
    start_directory = sys.argv[1]
    
    for dirpath, dirnames, filenames in os.walk(start_directory):
        for fname in filenames:
            if want_this_file(fname):
                fullname = os.path.join(dirpath, fname)
                prepend_file(fullname, dirpath)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have below code behind in c# if (Session[cmpDictionaryTitle]!= null) { downloadLinks.Text += @<li><a
I've been trying to decompress GIF's in PHP and seem to have everything except
I have created an object literal using the code below. Everything works fine. However,
I have below code: <a href=# id=@item.Id name=vote ><img src=/Content/images/021.png style=float:left alt= /></a> which
I have below code: class Program { static void Main(string[] args) { Task[] tasks
I have below code in html. <li class=selected runat=server id=lihome><a href=/ISS/home.aspx title=Home><span>Home</span></a></li> Now I
I have below code to insert a style into DOM (there is a use
I have below code.The logic here is if HostList conatains any blanck entry it
I have below code which overrides equals() and hashcode() methods. public boolean equals(Object obj)
I have below code snippet SimpleDateFormat dateFormat = new SimpleDateFormat( yyyy-MM-dd hh:mm:ss.SSS); String processedContentDate=2012-04-10

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.