Python – How can I detect if a user is using my file for the first time?

How can I detect if a user is using my file for the first time?… here is a solution to the problem.

How can I detect if a user is using my file for the first time?

I’m making a Python file and I want to show a different screen for the person opening the file for the first time, rather than repeat visitors. Like this:

if USER_NEW:
   print('New user screen')
else:
   print('other screen')

What should I do about it?

Solution

You need to store the information in another file so that you can read it later when your script runs again. When you run the script, you need to read this file and see if it contains a value to indicate that the visitor has already viewed the file, or write it to tell the program that they have already viewed it on their next visit.

For example:

with open('user.txt', 'r+') as file:

if file.read() == '':
        print('New user screen')
    else:

print('other screen')
        file.write('visited')

Related Problems and Solutions