我正在创建一个从压缩文件加载并运行 python 脚本的程序。除了这些 python 脚本之外,我还有一个配置文件,我之前使用 configparser 从程序的未压缩版本中加载信息。
是否可以使用configparser直接读取zip文件中的配置文件?或者我是否必须将其解压缩到临时文件夹中并从那里加载?
我尝试直接给出路径:
>>> sysconf = configparser.ConfigParser()
>>> sysconf.read_file("compressed.zip/config_data.conf")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/configparser.py", line 691, in read_file
self._read(f, source)
File "/usr/local/lib/python3.4/configparser.py", line 1058, in _read
raise MissingSectionHeaderError(fpname, lineno, line)
configparser.MissingSectionHeaderError: File contains no section headers.
file: '<???>', line: 1
没用。没有什么惊喜。
然后我尝试使用 zipfile
>>> zf = zipfile.ZipFile("compressed.zip")
>>> data = zf.read("config_data.conf")
>>> sysconf = configparser.ConfigParser()
>>> sysconf.read_file(data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/configparser.py", line 691, in read_file
self._read(f, source)
File "/usr/local/lib/python3.4/configparser.py", line 1009, in _read
if line.strip().startswith(prefix):
AttributeError: 'int' object has no attribute 'strip'
发现它也不起作用。
所以我不得不创建一个临时文件夹,解压到它,然后读取那里的conf文件。如果可能的话,我真的很想避免这种情况,因为conf文件是唯一的限制因素。此时我可以(并且正在)从 zip 文件加载 python 模块。
如果有办法将文件的原始文本直接传递给 configparser,我就可以获得该文件的原始文本,但搜索文档却空手而归。
更新: 我尝试使用 stringIO 作为文件对象,它似乎有点工作。 configparser 不会拒绝它,但它也不喜欢它。
>>> zf = zipfile.ZipFile("compressed.zip")
>>> data = zf.read(config_data.conf)
>>> confdata = io.StringIO(str(data))
>>> sysconf = configparser.ConfigParser()
>>> sysconf.readfp(confdata)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/configparser.py", line 736, in readfp
self.read_file(fp, source=filename)
File "/usr/local/lib/python3.4/configparser.py", line 691, in read_file
self._read(f, source)
File "/usr/local/lib/python3.4/configparser.py", line 1058, in _read
raise MissingSectionHeaderError(fpname, lineno, line)
configparser.MissingSectionHeaderError: File contains no section headers.
file: '<???>', line: 1
(continues to spit out the entire contents of the file)
如果我使用 read_file 代替,它不会出错,但也不会加载任何内容。
>>> zf = zipfile.ZipFile("compressed.zip")
>>> data = zf.read(config_data.conf)
>>> confdata = io.StringIO(str(data))
>>> sysconf = configparser.ConfigParser()
>>> sysconf.read_file(confdata)
>>> sysconf.items("General") #(this is the main section in the file)
Traceback (most recent call last):
File "/usr/local/lib/python3.4/configparser.py", line 824, in items
d.update(self._sections[section])
KeyError: 'General'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/configparser.py", line 827, in items
raise NoSectionError(section)
configparser.NoSectionError: No section: 'General'
最佳答案
block 引用>can get the raw text of the file if there's a way to pass that directly to configparser
尝试
configparser.ConfigParser.read_string
当与适当的 ZIP 文件结合使用时,此代码适用于我:
import zipfile import configparser zf = zipfile.ZipFile("compressed.zip") zf_config = zf.open("config_data.conf", "rU") zf_config_data = zf_config.read().decode('ascii') config = configparser.ConfigParser() config.read_string(zf_config_data) assert config['today']['lunch']=='cheeseburger'
经过深思熟虑,以下可能更合适:
import zipfile import configparser import io zf = zipfile.ZipFile("compressed.zip") zf_config = zf.open("config_data.conf", "rU") zf_config = io.TextIOWrapper(zf_config) config = configparser.ConfigParser() config.read_file(zf_config) assert config['today']['lunch']=='cheeseburger'
关于python - configparser 从 zip 加载配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31392057/