python 读取其他文件的变量 import 或 正则读取
例:从 ref.py 读取 路径变量
ref.py:
input="/aa/bb" script="/cc/dd"
则 1)可在目前的run.py:
import sys
sys.path.append("/path_ref/") #若ref.py不和当前run.py同路径,则添加ref.py的脚本路径。放心:本脚本外,sys.path不会改变。
from ref import input,script
2)也可以通过正则读取变量值,
import re
with open ("/path_ref/ref.py","r") as r:
p1=re.compile('\s*input\s*\=\s*(\S+)\s*')
p2=re.compile('\s*script\s*\=\s*(\S+)\s*')
for i in r:
if p1.search(i):
input1=p1.search(i).group(1)
if p2.search(i):
script=p2.search(i).group(1)
print input1,script
本文来自博客园,作者:BioinformaticsMaster,转载请注明原文链接:https://www.cnblogs.com/koujiaodahan/p/8398947.html
posted on 2018-02-01 14:42 BioinformaticsMaster 阅读(3378) 评论(0) 收藏 举报
浙公网安备 33010602011771号