from subprocess import call as subprocess_call
from os.path import isfile as file_exist
file = "/path/to/your/file"
if not file_exist(file):
# create file if exist
subprocess_call(["touch", file])
subprocess_call(["chmod", "777", file])
with open(file, "r") as f:
high_score = f.read()
这部分代码检查
file
是否存在。如果没有-创建此空文件并读取此文件我建议使用文件的完整路径,如
/home/user/folde/subfolder/file.txt
我希望这对您有所帮助。
请检查VS代码终端当前所在的文件夹是否是正在执行的python文件的父文件夹
解决方案:
您可以使用终端中的命令“
cd folder_name
”转到python文件的父文件夹:或者在设置文件“settings.json”中使用以下设置:
"python.terminal.executeInFileDir": true,
在执行run命令之前,它将自动转到当前文件的父文件夹:
尝试使用名为csv的模块 以下代码是读取文件的基本代码:
您可以使用任何变量
相关问题 更多 >
编程相关推荐