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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:23:59+00:00 2026-05-27T22:23:59+00:00

I need to update a file. I read it in and write it out

  • 0

I need to update a file. I read it in and write it out with changes. However, I’d prefer to write to a temporary file and rename it into place.

temp = tempfile.NamedTemporaryFile()
tempname = temp.name
temp.write(new_data)
temp.close()
os.rename(tempname, data_file_name)

The problem is that tempfile.NamedTemporaryFile() makes the temporary file in /tmp which is a different file system. This means os.rename() fails. If I use shlib.move() instead then I don’t have the atomic update that mv provides (for files in the same file system, yadda, yadda, etc.)

I know tempfile.NamedTemporaryFile() takes a dir parameter, but data_file_name might be "foo.txt" in which case dir='.'; or data_file_name might be "/path/to/the/data/foo.txt" in which case dir="/path/to/the/data".

What I’d really like is the temp file to be data_file_name + "some random data". This would have the benefit of failing in a way that would leave behind useful clues.

Suggestions?

  • 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-27T22:23:59+00:00Added an answer on May 27, 2026 at 10:23 pm

    You can use:

    • prefix to make the temporary file begin with the same name as the
      original file.
    • dir to specify where to place the temporary file.
    • os.path.split to split the directory from the filename.

    import tempfile
    import os
    dirname, basename = os.path.split(filename)
    temp = tempfile.NamedTemporaryFile(prefix=basename, dir=dirname)
    print(temp.name)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read, write and create an INI file with Python3. FILE.INI default_path
In Python I open a temporary file for write by using tempfile.mkstemp in order
I need to write and update XML file based on nodes. For that I
I need to be able to update my config file programmatically and change my
I need to update a combobox with a new value so it changes the
I need help loading a custom file format into my program made in c++...
I have a CSV file with many rows in which I need to update/replace
I am trying to figure out how to read/write lock a CSV or XLS
In a project I'm working on, I need to read from a CSV file,
Need help in figuring out, why the downloaded file size is 0 bytes? On

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.