Skip to content

Instantly share code, notes, and snippets.

@starkayc
Created April 11, 2025 01:00
Show Gist options
  • Select an option

  • Save starkayc/bfdde43b06731316da10f4f63b4a6d76 to your computer and use it in GitHub Desktop.

Select an option

Save starkayc/bfdde43b06731316da10f4f63b4a6d76 to your computer and use it in GitHub Desktop.
Increases the number after each run.
import os
file_path = "counter.txt" # You can also use an absolute path if needed
# Make sure the file exists and read the current count
if not os.path.exists(file_path):
count = 0
else:
with open(file_path, "r") as f:
content = f.read().strip()
if content.isdigit():
count = int(content)
else:
count = 0
# Increment the count
count += 1
# Write the new count back to the file
with open(file_path, "w") as f:
f.write(str(count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment