import os
student_path = os.environ['STUDENT_PATH']
faculty_path = os.environ['FACULTY_PATH']
print(student_path, faculty_path)
Personal opinion : I don't like to configure my scripts using environment variables as the ones you chose may be used by another software + it is always messy to debug
Personal opinion : I use arguments when I want to control a small amount of parameters on my scripts to change its behavior (verbose, nb of cpus, ...).
Personal opinion : I use config files when I need to adapt parameters when deploying on new machines. I don't like to use arguments for this purpose as I don't want to specify the same values every time I use my script (often these kind of parameters don't have good default values).
import os
start = "/home/"
for dirpath, dirnames, filenames in os.walk(start):
found = False
for dirname in dirnames:
if dirname == 'Student':
full_path = os.path.join(dirpath, dirname)
found = True
break
if found:
break
对于这类问题,您有多种解决方案:
在每台机器上创建环境变量,在脚本中执行以下操作:
使用arguments
然后像这样调用脚本,并根据机器调整这行
创建配置文件并使用configparser
你知道吗配置.ini文件
脚本用法:
然后在每台机器上部署不同的
config.ini
文件(像ansible这样的工具可以帮助您实现自动化)创建模块
您还可以创建一个模块来存储这些参数,而不是一个配置文件。你知道吗
我的_配置.py你知道吗
然后导入
你知道吗脚本.py你知道吗
可以使用walk库查找目标文件夹路径。如果每个搜索名称只有一个文件夹,则效果最佳:
输出:
/home/../学生
相关问题 更多 >
编程相关推荐