StringIO.getvalue return content of StringIO:
>>> import StringIO
>>> f = StringIO.StringIO()
>>> f.write('asdf')
>>> f.getvalue()
'asdf'
Alternatively, you can change position of the file using seek:
>>> f.read()
''
>>> f.seek(0)
>>> f.read()
'asdf'
Try following:
mf = StringIO.StringIO()
with zipfile.ZipFile(mf, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
zf.writestr('file1.txt', "hi")
zf.writestr('file2.txt', "hi")
with open("C:/path/my_zip.zip", "wb") as f: # use `wb` mode
f.write(mf.getvalue())
Answer from falsetru on Stack Overflow
Videos
StringIO.getvalue return content of StringIO:
>>> import StringIO
>>> f = StringIO.StringIO()
>>> f.write('asdf')
>>> f.getvalue()
'asdf'
Alternatively, you can change position of the file using seek:
>>> f.read()
''
>>> f.seek(0)
>>> f.read()
'asdf'
Try following:
mf = StringIO.StringIO()
with zipfile.ZipFile(mf, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
zf.writestr('file1.txt', "hi")
zf.writestr('file2.txt', "hi")
with open("C:/path/my_zip.zip", "wb") as f: # use `wb` mode
f.write(mf.getvalue())
Modify falsetru's answer for python3
1) use io.StringIO instead of StringIO.StringIO
StringIO in python3
2) use b"abc" instead of "abc" , or
python 3.5: TypeError: a bytes-like object is required, not 'str' when writing to a file
3) encode to binary string str.encode(s, "utf-8")
Best way to convert string to bytes in Python 3?
import zipfile
import io
mf = io.BytesIO()
with zipfile.ZipFile(mf, mode="w",compression=zipfile.ZIP_DEFLATED) as zf:
zf.writestr('file1.txt', b"hi")
zf.writestr('file2.txt', str.encode("hi"))
zf.writestr('file3.txt', str.encode("hi",'utf-8'))
with open("a.txt.zip", "wb") as f: # use `wb` mode
f.write(mf.getvalue())
This should also work for gzip: How do I gzip compress a string in Python?
It is strongly advised to use a context manager. As an advantage, it is made sure the file is always closed, no matter what:
with open("Output.txt", "w") as text_file:
text_file.write("Purchase Amount: %s" % TotalAmount)
This is the explicit version (but always remember, the context manager version from above should be preferred):
text_file = open("Output.txt", "w")
text_file.write("Purchase Amount: %s" % TotalAmount)
text_file.close()
If you're using Python2.6 or higher, it's preferred to use str.format()
with open("Output.txt", "w") as text_file:
text_file.write("Purchase Amount: {0}".format(TotalAmount))
For python2.7 and higher you can use {} instead of {0}
In Python3, there is an optional file parameter to the print function
with open("Output.txt", "w") as text_file:
print("Purchase Amount: {}".format(TotalAmount), file=text_file)
Python3.6 introduced f-strings for another alternative
with open("Output.txt", "w") as text_file:
print(f"Purchase Amount: {TotalAmount}", file=text_file)
In case you want to pass multiple arguments you can use a tuple
price = 33.3
with open("Output.txt", "w") as text_file:
text_file.write("Purchase Amount: %s price %f" % (TotalAmount, price))
More: Print multiple arguments in python
Using utils::zip which comes with base R, so usually no need to load library(utils).
writeLines(mytext, "a.txt")
zip(zipfile="filename.zip", files="a.txt", flags="-u -9")
-uupdate: only changed or new files (i.e., appends)-9compress better (i.e., DEFLATE)
Note: unzip -v filename.zip in console shows "Stored" under Method unless compression is beneficial, in which case it shows sth like "Defl:X".
If you want to remove the interim file, you can use unlink("a.txt").
First note that the way utils::zip in R works is that it calls an external zip utility using system2 so we can mimic that ourself.
I am using 7-zip, version 21.07 (x64) 2021-12-26 on Windows. Its executable name is 7z.exe and it supports the -si flag to take input from stdin and -tzip to specify zip format so for me this works:
mytext <- 'abcdefghijklmnopqrstuvwxyz'
system2("7z.exe", c("a", "-tzip", "filename.zip", "-sia.txt"), input = mytext)
Modify this appropriately if you are using a different zip command.