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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:40:15+00:00 2026-05-12T10:40:15+00:00

Currently I have a loop that tries to find an unused filename by adding

  • 0

Currently I have a loop that tries to find an unused filename by adding suffixes to a filename string. Once it fails to find a file, it uses the name that failed to open a new file wit that name. Problem is this code is used in a website and there could be multiple attempts to do the same thing at the same time, so a race condition exists.

How can I keep python from overwriting an existing file, if one is created between the time of the check and the time of the open in the other thread.

I can minimize the chance by randomizing the suffixes, but the chance is already minimized based on parts of the pathname. I want to eliminate that chance with a function that can be told, create this file ONLY if it doesn’t exist.

I can use win32 functions to do this, but I want this to work cross platform because it will be hosted on linux in the end.

  • 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-12T10:40:15+00:00Added an answer on May 12, 2026 at 10:40 am

    If you are concerned about a race condition, you can create a temporary file and then rename it.

    >>> import os
    >>> import tempfile
    >>> f = tempfile.NamedTemporaryFile(delete=False)
    >>> f.name
    'c:\\users\\hughdb~1\\appdata\\local\\temp\\tmpsmdl53'
    >>> f.write("Hello world")
    >>> f.close()
    >>> os.rename(f.name, r'C:\foo.txt')
    >>> if os.path.exists(r'C:\foo.txt') :
    ...     print 'File exists'
    ...
    File exists
    

    Alternatively, you can create the files using a uuid in the name. Stackoverflow item on this.

    >>> import uuid
    >>> str(uuid.uuid1())
    '64362370-93ef-11de-bf06-0023ae0b04b8'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a database that within a loop checks a table if a
Currently, I have a while loop for listening the port. When a socket accepta
I'm currently making a C++ game. I have my main loop during which the
Currently in my code I have something in a for loop similar to: bstr
I currently have a MySQL database that I am accessing using PHP. You search
I currently have the below code, but obviously I am not that skilled on
I have a flash .swf file that I embed on my webpage. On my
I have a foreach loop that starts a process within a try/catch. In the
I have a text file that contains the following data. The first line is
I have a combo box that I want to load with a string array

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.