我正在用python制作一个文本编辑器,我想在编辑器中添加一个Variable Explorer特性,但我无法从python文件中提取变量值。我的基本工作原理是,我的程序获取当前编辑文件的位置并尝试导入它,但我无法导入,因为它不是一个对象。它有点混乱,所以让我看看代码。你知道吗
fileName='C:\Users\Project.py'
class varExplorer:
def ShowVarList(editfile):
editfile.replace('\','.')
editfile.replace('.py','')
editfile.replace(':','')
# so the file path will be like C.Users.Project
import editfile # the problem
print(editfile.__dict__)# here i will get dictionary of values
varExplorer.ShowVarList(fileName)
为dict提供帮助
print(editfile.__dict__)
import editfile # the problem
所以我需要一个函数,它可以从任何位置打印特定python文件中的所有变量及其值。你知道吗
使用
importlib
还要小心,
str
在Python中是不可变的,replace
返回一个新字符串,并且不修改它的参数。 所以你得到:相关问题 更多 >
编程相关推荐