我有一個tar文件,裏面有一些文件。 我需要編寫一個python腳本,它將讀取文件的內容,並給出包括總字符數,空格,換行符,所有內容在內的總數,而不需要解壓tar文件。在沒有解壓縮的情況下讀取tar文件內容,在python腳本中
回答
你可以使用getmembers()
>>> import tarfile
>>> tar = tarfile.open("test.tar")
>>> tar.getmembers()
之後,你可以使用extractfile()的成員提取物作爲文件對象。只是一個例子
import tarfile,os
import sys
os.chdir("/tmp/foo")
tar = tarfile.open("test.tar")
for member in tar.getmembers():
f=tar.extractfile(member)
content=f.read()
print "%s has %d newlines" %(member, content.count("\n"))
print "%s has %d spaces" % (member,content.count(" "))
print "%s has %d characters" % (member, len(content))
sys.exit()
tar.close()
隨着在上面的例子中文件對象「F」,你可以使用read(),readlines方法()等
「可以改爲」for tar for成員「,它可以是一個生成器或一個迭代器(我不知道是哪一個)。但它一次只能獲得一個成員。 – huggie 2011-12-28 09:24:04
我剛剛有一個類似的問題,但tarfile模塊似乎吃我的內存,即使我用'r |''選項。 – devsnd 2012-05-21 17:39:52
啊。我解決了它。假設你會像huggie暗示的那樣編寫代碼,你必須偶爾「清除」成員列表。因此,考慮到上面的代碼示例,這將是'tar.members = []'。更多信息在這裏:http://bit.ly/JKXrg6 – devsnd 2012-05-21 17:45:51
您需要使用tarfile模塊。具體而言,您使用類tar文件的實例與TarFile.getnames()
| getnames(self)
| Return the members of the archive as a list of their names. It has
| the same order as the list returned by getmembers().
訪問該文件,然後訪問的名稱相反,如果你想閱讀的內容,那麼你用這個方法
| extractfile(self, member)
| Extract a member from the archive as a file object. `member' may be
| a filename or a TarInfo object. If `member' is a regular file, a
| file-like object is returned. If `member' is a link, a file-like
| object is constructed from the link's target. If `member' is none of
| the above, None is returned.
| The file-like object is read-only and provides the following
| methods: read(), readline(), readlines(), seek() and tell()
請注意,您可以通過像myFile = myArchive.extractfile(dict(myArchive.getnames(),myArchive.getmembers()))['path/to/file'])構造的索引訪問成員。對於tar.getmembers()中的成員,read()' – ThorSummoner 2014-04-26 07:28:23
由@斯特凡諾 - 博里尼 提到的方法的實施方案通過文件名訪問tar歸檔成員,像這樣
#python3
myFile = myArchive.extractfile(
dict(zip(
myArchive.getnames(),
myArchive.getmembers()
))['path/to/file']
).read()`
現金
dict(zip(
從https://stackoverflow.com/a/209854/1695680tarfile.getnames
從https://stackoverflow.com/a/2018523/1695680- 此外,對於我的用途,閱讀從緩衝How to construct a TarFile object in memory from byte buffer in Python 3?
- 1. 如何在不用java解壓縮的情況下讀取壓縮文件的內容
- 2. 在沒有解壓縮的情況下包含ant build xml內聯jar文件?
- 3. 如何在沒有解壓縮的情況下從.gz壓縮文件中獲取幾行
- 4. Node.js在不解壓的情況下讀取zip文件
- 5. 如何在沒有先將S3複製到服務器的情況下在S3上壓縮/ tar文件?
- 6. 如何在不解壓縮內容的情況下在.tar.gz壓縮文件中查看特定文件的內容?
- 7. 使用golang閱讀tar文件的內容而不解壓
- 8. 在python中壓縮和解壓縮文本文件
- 9. 在不改變縮進的情況下壓縮文本
- 10. 在沒有getopts的情況下在shell腳本中添加使用內容
- 11. 在通配符的Python腳本中解壓縮文件
- 12. tar文件沒有完全解壓
- 13. 如何列出Java中壓縮的tar文件的內容
- 14. 在沒有Oracle Lite的情況下讀取Oracle Lite ODB文件
- 15. 如何在不解壓縮的情況下列出zip壓縮文件中的文件?
- 16. 在沒有InAppPurchase的情況下解鎖內容
- 17. 使用C#解壓縮tar/BZ2文件
- 18. 使用C#解壓縮tar文件
- 19. 在沒有線程的情況下同時執行python腳本
- 20. 在沒有擴展名的情況下使用Python讀取文件
- 21. 在沒有GUI的情況下獲取iPhone地址簿內容
- 22. 解壓Z01,Z02,壓縮在Python文件
- 23. Remux RTSP流入容器? (在沒有解碼的情況下寫入讀取幀)
- 24. 如何從tar中讀取gz壓縮文件
- 25. 解壓縮文件的內容
- 26. 在沒有完整下載的情況下閱讀zip文件
- 27. 上傳壓縮文件,解壓縮並讀取文件
- 28. 如何在沒有csv.writer的情況下在python中創建文本文件?
- 29. 如何使Eclipse在沒有Eclipse-BundleShape標頭的情況下解壓縮包
- 30. 在沒有es6腳本的情況下獲取prevstate反應
你怎麼能指望的字符/字母/空間/無解壓那些別處寄託都tar歸檔? – YOU 2010-01-07 06:17:35
這正是問題所在。 – 2013-01-15 14:34:43