-
Notifications
You must be signed in to change notification settings - Fork 1
/
count_zipfile_contents.py
64 lines (45 loc) · 1.75 KB
/
count_zipfile_contents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 4 15:21:57 2020
@author: eneemann
This script is used to count the number of files within zipped folder of a directory
"""
# Import Libraries
import os
import time
import zipfile
# Start timer and print start time in UTC
start_time = time.time()
readable_start = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
print("The script start time is {}".format(readable_start))
today = time.strftime("%Y%m%d")
#-----------------------Start Main Code------------------------#
# Set up variables
data_dir = r"G:\Shared drives\AGRC Projects\PLSS Fabric\Data\CountyData\Rich\Rich County Tie Sheets"
#-----------------------Functions------------------------#
def count_files(directory, total=0):
os.chdir(directory)
zip_list = []
for file in os.listdir(directory):
if file.endswith(".zip"):
zip_list.append(file)
print("List of .zip files:")
for zip in zip_list:
print(zip)
for zip in zip_list:
print("Unzipping {} ...".format(zip))
with zipfile.ZipFile(zip,"r") as zip_ref:
count = len(zip_ref.infolist())
total += count
print(f'adding {count} to pdf_total')
return total
#-----------------------Call Functions-----------------------#
print(f'Counting contents of zipped files in {data_dir} ...')
pdf_total = count_files(data_dir)
print(f'Total number of PDF files: {pdf_total}')
#-----------------------End Main Code------------------------#
print("Script shutting down ...")
# Stop timer and print end time in UTC
readable_end = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
print("The script end time is {}".format(readable_end))
print("Time elapsed: {:.2f}s".format(time.time() - start_time))