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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:02:52+00:00 2026-05-15T18:02:52+00:00

Whats the best to make a job ID counter in python? For example if

  • 0

Whats the best to make a job ID counter in python? For example if a Job ID startd of with value of “0” and everytime someone ran the script the counter went up by one?

E.G.

X = 0

Perform_some_process

x +=1

Now the value of x will be one, but if i ran the script again x will be equal to one again and not two. How would make so when someone ran the script for the second time x is equal to two? and so forth for the future runs.

  • 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-15T18:02:53+00:00Added an answer on May 15, 2026 at 6:02 pm

    You need to “persist” that counter — simplest is to use a file for the purpose. For example:

    import os
    
    def onemore():
        f = __file__ + '.counter'
        if os.path.exists(f):
            with open(f) as thefile:
                counter = int(thefile.read())
        else:
            counter = -1
        counter += 1
        with open(f, 'w') as thefile:
            thefile.write(str(counter) + '\n')
        return counter
    

    This could give problems (missing some counts) if multiple instances of the script are starting at exactly the same time — if that’s an issue for you please let us know (and let us know what OS you need to work on) to learn more about how to perform locking on that crucial file.

    This solution also assumes that the directory where this Python (or bytecode) file exists is writable by the user under whose identity the script is running. If that’s a problem, the script needs some configuration file indicating which directory is guaranteed to be writable by any user who may be running the script (and will use that directory to form the string f, the name of the counter file).

    Note that I’ve kept the contents of the counter file in easily human-readable form — that’s for ease of debugging (and a tiny price to pay when one number is all that’s in the file). It would of course be possible to keep the file in binary form instead.

    If your script needs to persist many bits and pieces of info, a sqlite database file is probably the handiest way to keep them all together.

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

Sidebar

Related Questions

I am wondering whats the best way to programmatically make rounded corners for images.
what's the best way to make communications between page widgets residing on differents ViewPart?
What's the best way to make a function that has pointer as argument work
What is the best way to make the size of box 2 equal to
What is the best way to make an HTTP GET request in Ruby with
What is a best approach to make a function or set of statements thread
What is the best way to make sure that all the environment variables I
What is the best way to make my Qt Ruby application into an executable
I'm wondering what is the best way to make data thread-safe. Specifically, I need
What is the best solution if I want to make a OnTouch event repeat

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.