gpt4 book ai didi

python - 如果文件名更改则中止执行

转载 作者:行者123 更新时间:2023-11-30 23:41:09 25 4
gpt4 key购买 nike

这是我老师问的一个问题。 “如果 Filenamed 被用户重命名,您将如何阻止脚本执行。”

我心里已经有了这个想法。

#consider the name of the script to be myscript.py
import os
signal = 0
while 1:
if "myscript.py" not in os.listdir("."):
signal = 1
else:
if signal:
break
else:
#do the requirements

但是他说,如果用户目录中已经有一个名为“myscript.py”的文件,并且他更改了脚本的名称,则脚本将运行良好。然后我就被困住了。

我使用的是python2.7,操作系统:Windows,如果需要,允许使用任何外部库,有什么建议吗?

最佳答案

模块的 __file__ 属性包含包含它的 Python 脚本的完整路径名。

关于python - 如果文件名更改则中止执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12230093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com