Message225389
| Author |
lars.gustaebel |
| Recipients |
lars.gustaebel, markgrandi |
| Date |
2014年08月16日.10:53:45 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1408186425.77.0.539187713044.issue22208@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
Why overcomplicate things?
import io, tarfile
with tarfile.open("foo.tar", mode="w") as tar:
b = "hello world!".encode("utf-8")
t = tarfile.TarInfo("helloworld.txt")
t.size = len(b) # this is crucial
tar.addfile(t, io.BytesIO(b))
My answer to issue10369 was never supposed to be used as a reference on how to add file-like objects to a TarFile. I posted it as a simpler but equivalent version of the code of the original poster, which is why it looks "hackish".
I think the documentation on TarFile.gettarinfo() is rather clear on how to use it (i.e. that it needs a file object with a valid file descriptor). Also, I think that the code above is intuitive and simple. |
|
History
|
|---|
| Date |
User |
Action |
Args |
| 2014年08月16日 10:53:45 | lars.gustaebel | set | recipients:
+ lars.gustaebel, markgrandi |
| 2014年08月16日 10:53:45 | lars.gustaebel | set | messageid: <1408186425.77.0.539187713044.issue22208@psf.upfronthosting.co.za> |
| 2014年08月16日 10:53:45 | lars.gustaebel | link | issue22208 messages |
| 2014年08月16日 10:53:45 | lars.gustaebel | create |
|