输入后存储用户定义的数据

发布于 2024-09-15 11:32:05 字数 220 浏览 1 评论 0原文

我正在制作一个Python程序,我想检查用户是否第一次运行该程序(firstTime == True)。然而,在运行之后,我想将firstTime永久更改为False。 (如果是第一次运行,我想输入其他变量,这些变量将保留,但应该以相同的方式解决)。

有没有比从包含数据的文件中读取更好的方法?如果没有,我如何找到文件的运行位置(这样数据将位于同一目录中)?

I am making a python program, and I want to check if it is the users first time running the program (firstTime == True). After its ran however, I want to permanently change firstTime to False. (There are other variables that I want to take input for that will stay if it is the first run, but that should be solved the same way).

Is there a better way then just reading from a file that contains the data? If not, how can I find where the file is being ran from (so the data will be in the same dir)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

输什么也不输骨气 2024-09-22 11:32:05

如果您想持久保存数据,它将“最终”保存到磁盘文件中(尽管可能存在中间步骤,例如通过网络或数据库系统,最终如果数据要持久保存,它将保存在磁盘文件中的某个位置)。

“找出你在哪里”,

import os
print os.path.dirname(os.path.abspath(__file__))

有多种变体,但这是基本思想。任何 .py 脚本或模块中的 __file__ 都会给出该文件所在的文件路径(当然,在交互式命令行上不起作用,因为不涉及任何文件)然后;-)。

Python 标准库中的 os.path 模块有许多有用的函数来操作路径字符串 - 在这里,我们使用两个:abspath 给出绝对(而不是相对)文件路径的版本,因此您不必关心当前的工作目录是什么;和 dirname 仅提取目录名称(实际上是整个目录路径;-)并删除正确的文件名(您不关心模块的名称是否为 foo.pybar.py,仅在其所在目录中;-)。

If you want to persist data, it will "eventually" be to disk files (though there might be intermediate steps, e.g. via a network or database system, eventually if the data is to be persistent it will be somewhere in disk files).

To "find out where you are",

import os
print os.path.dirname(os.path.abspath(__file__))

There are variants, but this is the basic idea. __file__ in any .py script or module gives the file path in which that file resides (won't work on the interactive command line, of course, since there's no file involved then;-).

The os.path module in Python's standard library has many useful function to manipulate path strings -- here, we're using two: abspath to give an absolute (not relative) version of the file's path, so you don't have to care about what your current working directory is; and dirname to extract just the directory name (actually, the whole directory path;-) and drop the filename proper (you don't care if the module's name is foo.py or bar.py, only in what directory it is;-).

偏爱你一生 2024-09-22 11:32:05

如果程序第一次运行,只需在同一目录中创建文件就足够了(当然可以删除该文件以便再次运行,但这有时很有用):

firstrunfile = 'config.dat'
if not  os.path.exists(firstrunfile):
    ## configuration here
    open(firstrunfile,'w').close() ## .write(configuration)
    print 'First run'
    firstTime == True
else:
    print 'Not first run'
    ## read configuration
    firstTime == False

It is enough to just create file in same directory if program is run first time (of course that file can be deleted to do stuff for first run again, but that can be sometimes usefull):

firstrunfile = 'config.dat'
if not  os.path.exists(firstrunfile):
    ## configuration here
    open(firstrunfile,'w').close() ## .write(configuration)
    print 'First run'
    firstTime == True
else:
    print 'Not first run'
    ## read configuration
    firstTime == False
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文