Python读取文件模块
我的当前目录中有一个 .zip 文件夹。如何告诉 python 读取 .zip 文件夹中的“contents”文件?内容文件包含 zip 存档中所有文件的名称。
我基本上希望Python读取内容文件,查找特定项目,例如单词“程序徽标”并将“程序徽标”的相应值记录到内存中。
例如,python 应该查询内容文件并停止,直到到达“程序徽标”一词。我已经分配了一个值,例如程序徽标的123-123-23。我如何告诉 python 将该值记录到变量中?我应该使用Python中的读取文件模块吗?
I have a .zip folder in my current directory. How can I tell python to read the "contents" file in the .zip folder? The contents file has the names of all files in the zip archive.
I basically want Python to read the contents file, look for a specific item, such as the word " program logo" and record to memory the corresponding value for "program logo".
For example, python should query the contents file and stop until it reaches the words "program logo". I have assigned a value, such as 123-123-23 for program logo. How can I tell python to record that value to a variable? Should I use the read file module in python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Zip 文件不是文件夹,而是档案。它们是文件,您必须像其他文件一样打开它们进行阅读。
我认为您应该查找 zipfile 模块。如果您的 zip 中确实有一个名为“contents”的文件,那么
我不确定这是否是您想要的,但它向您展示了如何查看 .zip 存档内部。
PS您使用的是什么操作系统?是什么让您认为“.zip”是一个文件夹?您如何看待这个“内容”文件?您如何创建“内容”文件 - 它是文本文件吗?特别是相关行(包含“程序徽标”)是什么样子?没有这些信息我们无法回答这个问题!
Zip files are not folders, they are archives. They are files, and you have to open them for reading, like any other files.
I reckon you should look up the
zipfile
module. If you actually have a file called "contents" in your zip, thenI'm not sure this is what you want, but it shows you how to look inside a .zip archive.
PS what OS are you using and what made you think that the ".zip" is a folder? How are you seeing this "contents" file? How did you create the "contents" file - is it a text file? and in particular, what does the relevant line (containing "program logo") look like? Without this information we can't answer the question!