Nối tiếp tutorial về lập trình Python, hôm nay chúng ta sẽ học cách tạo ra file zip bằng cách sử dụng Python, thông qua thư viện zipfile. Thư viện này là build-in của Python 3 nên các bạn không cần phải thực hiện cài đặt.
Tạo File Zip sử dụng Python
Đầu tiên là import thư viện filezip
import zipfile
Lựa chọn mode để nén file zip
# Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print[ex]
Tạo file Zip với đường dẫn và mode [w – write, a – append]
# create the zip file first parameter path/name, second mode zf = zipfile.ZipFile['vinasupport.com.zip', mode='w']
Thêm file vào file zip vừa tạo
# Add file to the zip file # first parameter file to zip, second filename in zip zf.write['vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression
Close file
# Don't forget to close the file! zf.close[]
Vậy đoạn code đầy đủ sẽ là:
import zipfile # Select the compression mode ZIP_DEFLATED for compression # or zipfile.ZIP_STORED to just store the file try: import zlib compression = zipfile.ZIP_DEFLATED except Exception as ex: compression = zipfile.ZIP_STORED print[ex] # create the zip file first parameter path/name, second mode zf = zipfile.ZipFile['vinasupport.com.zip', mode='w'] # Add file to the zip file # first parameter file to zip, second filename in zip zf.write['vinasupport.com.txt', 'vinasupport.com.txt', compress_type=compression] # Don't forget to close the file! zf.close[]
Kết quả
Sau khi chạy đoạn code trên chúng ta đã tạo thành công file zip
Đây là chương trình tạo file zip đơn giản, các bạn muốn làm nhiều thư hơn như zip folder, tạo mật khẩu cho file zip thì vui lòng tham khảo hướng dẫn sử dụng thư viện zipfile ở đây.
Nguồn vinasupport.com
Here's how you can access the indices and array's elements using for-in loops.
Nội dung chính Show
- 1. Looping elements with counter and += operator.
- 2. Looping elements using enumerate[] method.
- 3. Using index and value separately.
- 4. You can change the index number to any increment.
- 5. Automatic counter incrementation with range[len[...]].
- 6. Using for-in loop inside function.
- 7. Of course, we can't forget about while loop.
- 8. yield statement returning a generator object.
- 9. Inline expression with for-in loop and lambda.
- For loops in other languages
- Looping in Python
- range of length
- for-in: the usual way
- What if we need indexes?
- range of length
- What if we need to loop over multiple things?
- Looping cheat sheet
- Practice makes perfect
- What is index in for loop Python?
- How do you iterate through an index in Python?
- How do you define for loop index?
- How do you use the index function in Python?
1. Looping elements with counter and += operator.
items = [8, 23, 45, 12, 78]
counter = 0
for value in items:
print[counter, value]
counter += 1
Result:
# 0 8
# 1 23
# 2 45
# 3 12
# 4 78
2. Looping elements using enumerate[] method.
items = [8, 23, 45, 12, 78]
for i in enumerate[items]:
print["index/value", i]
Result:
# index/value [0, 8]
# index/value [1, 23]
# index/value [2, 45]
# index/value [3, 12]
# index/value [4, 78]
3. Using index and value separately.
items = [8, 23, 45, 12, 78]
for index, value in enumerate[items]:
print["index", index, "for value", value]
Result:
# index 0 for value 8
# index 1 for value 23
# index 2 for value 45
# index 3 for value 12
# index 4 for value 78
4. You can change the index number to any increment.
items = [8, 23, 45, 12, 78]
for i, value in enumerate[items, start=1000]:
print[i, value]
Result:
# 1000 8
# 1001 23
# 1002 45
# 1003 12
# 1004 78
5. Automatic counter incrementation with range[len[...]].
items = [8, 23, 45, 12, 78]
for i in range[len[items]]:
print["Index:", i, "Value:", items[i]]
Result:
# ['Index:', 0, 'Value:', 8]
# ['Index:', 1, 'Value:', 23]
# ['Index:', 2, 'Value:', 45]
# ['Index:', 3, 'Value:', 12]
# ['Index:', 4, 'Value:', 78]
6. Using for-in loop inside function.
items = [8, 23, 45, 12, 78]
def enum[items, start=0]:
counter = start
for value in items:
print[counter, value]
counter += 1
enum[items]
Result:
# 0 8
# 1 23
# 2 45
# 3 12
# 4 78
7. Of course, we can't forget about while loop.
items = [8, 23, 45, 12, 78]
counter = 0
while counter < len[items]:
print[counter, items[counter]]
counter += 1
Result:
# 0 8
# 1 23
# 2 45
# 3 12
# 4 78
8. yield statement returning a generator object.
def createGenerator[]:
items = [8, 23, 45, 12, 78]
for [j, k] in enumerate[items]:
yield [j, k]
generator = createGenerator[]
for i in generator:
print[i]
Result:
# [0, 8]
# [1, 23]
# [2, 45]
# [3, 12]
# [4, 78]
9. Inline expression with for-in loop and lambda.
items = [8, 23, 45, 12, 78]
xerox = lambda upperBound: [[i, items[i]] for i in range[0, upperBound]]
print[xerox[5]]
Result:
# [[0, 8], [1, 23], [2, 45], [3, 12], [4, 78]]
If you’re moving to Python from C or Java, you might be confused by Python’s for
loops. Python doesn’t actually have for loops… at least not the same kind of for
loop that C-based languages have. Python’s for
loops are actually foreach loops.
In this article I’ll compare Python’s for
loops to those of other languages and discuss the usual ways we solve common problems with
for
loops in Python.
For loops in other languages
Before we look at Python’s loops, let’s take a look at a for loop in JavaScript:
1 2 3 4 |
|