I'm making a Python file, and I want a different screen to show up for someone who is opening the file for the very first time, other than a recurring visitor. Like so:
我正在制作一个Python文件,我想要一个不同的屏幕显示给第一次打开文件的人,而不是一个重复访问者。像这样:
if USER_NEW:
print('New user screen')
else:
print('other screen')
Exactly how would I go about this?
究竟我该怎么做呢?
2 个解决方案
#1
2
You would need to store the information in another file to read later, when your script is run again. When you run your script you need to read this file and see if contains a value to say the visitor has already looked at the file or write in it to tell the program when they have during the 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')
#2
1
You can save program information in a state file. Where to do that depends on the operating system and even then you'll find disagreement. On windows you can use the environment variable %LOCALAPPDATA%
. On most unixy systems you can use ~/.myapp
or perhaps ~/.config/myapp
(I like the second much better). I don't know the conventions on other machines.
您可以将程序信息保存在状态文件中。这样做取决于操作系统,即使这样你也会发现不同意见。在Windows上,您可以使用环境变量%LOCALAPPDATA%。在大多数unixy系统上你可以使用〜/ .myapp或者〜/ .config / myapp(我更喜欢第二种)。我不知道其他机器上的约定。
You also need to establish a convention for the format of the file. I'm just going to look for a file name.
您还需要为文件格式建立约定。我只想找一个文件名。
import platform
import os
import time
if platform.system() == "Windows":
my_app_path = os.path.expandvars("%LOCALAPPDATA%/myapp")
elif platform.system() == "Linux":
my_app_path = os.path.expanduser("~/.config/myapp")
else:
exit(2)
first_run_file = os.path.join(my_app_path, "first_run.txt")
if not os.path.exists(first_run_file):
first_run = True
os.makedirs(my_app_path)
open(first_run_file, "w")
else:
first_run = False
#1
2
You would need to store the information in another file to read later, when your script is run again. When you run your script you need to read this file and see if contains a value to say the visitor has already looked at the file or write in it to tell the program when they have during the 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')
#2
1
You can save program information in a state file. Where to do that depends on the operating system and even then you'll find disagreement. On windows you can use the environment variable %LOCALAPPDATA%
. On most unixy systems you can use ~/.myapp
or perhaps ~/.config/myapp
(I like the second much better). I don't know the conventions on other machines.
您可以将程序信息保存在状态文件中。这样做取决于操作系统,即使这样你也会发现不同意见。在Windows上,您可以使用环境变量%LOCALAPPDATA%。在大多数unixy系统上你可以使用〜/ .myapp或者〜/ .config / myapp(我更喜欢第二种)。我不知道其他机器上的约定。
You also need to establish a convention for the format of the file. I'm just going to look for a file name.
您还需要为文件格式建立约定。我只想找一个文件名。
import platform
import os
import time
if platform.system() == "Windows":
my_app_path = os.path.expandvars("%LOCALAPPDATA%/myapp")
elif platform.system() == "Linux":
my_app_path = os.path.expanduser("~/.config/myapp")
else:
exit(2)
first_run_file = os.path.join(my_app_path, "first_run.txt")
if not os.path.exists(first_run_file):
first_run = True
os.makedirs(my_app_path)
open(first_run_file, "w")
else:
first_run = False