是否可以从外部cfg
(配置)文件读取路径。你知道吗
我正在制作一个打开文件的应用程序。目前我必须复制和粘贴路径多次。我想在我的cfg
文件中编写路径并从Python程序中调用它。你知道吗
这是我的Python文件:
import ConfigParser
import os
class Messaging(object):
def __init__(self):
self.config = ConfigParser.RawConfigParser()
self.rutaExterna = os.path.join(os.getcwd(), "app/properties.cfg")
self.config.read(['properties.cfg', self.rutaExterna])
def net(self):
# with open('/etc/network/interfaces', 'r+') as f:
direccion = self.config.read('direccion', 'enlace')
with open('direccion') as f:
for line in f:
found_network = line.find('network')
if found_network != -1:
network = line[found_network+len('network:'):]
print ('network: '), network
return network
CFG文件:
[direccion]
enlace = '/etc/network/interfaces', 'r+'
我想将文件路径存储在cfg
文件的变量中。你知道吗
然后我可以使用Python文件中的变量打开该文件。你知道吗
配置解析器支持读取目录。你知道吗
一些例子: https://wiki.python.org/moin/ConfigParserExamples
更新了CFG文件(我已经从配置文件中删除了r+)
CFG文件:
更新的Python代码:
使用
self.config.get('direccion','enlace')
而不是self.config.read('direccion', 'enlace')
,然后可以split()
和strip()
字符串并将它们作为参数传递给open()
:此外,配置文件中不需要
'
:测试了这个,它工作了。你知道吗
相关问题 更多 >
编程相关推荐