需求

将变量写在一个文件中,脚本每次读取文件中的变量进行处理。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 读取文件
file_path = 'input.txt'

try:
with open(file_path, 'r') as file:
# 从文件中读取变量
variable_to_process = file.read()
print(f"从文件中读取的变量值为: {variable_to_process}")

# 在这里可以对变量进行处理
# ...

except FileNotFoundError:
print(f"找不到文件: {file_path}")
except Exception as e:
print(f"发生了错误: {e}")