-
Notifications
You must be signed in to change notification settings - Fork 16
/
solution_41.py
37 lines (26 loc) · 1.11 KB
/
solution_41.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
# Exercise 4.1
import os # Import the os module into the global namespace.
def count_files(dir_name):
"""Counts files present in the input directory.
Only files are counted, directories are ignored.
"""
# Initialize file counter.
file_count = 0
# Loop through all files and directories present in the input directory.
for file in os.listdir(path=dir_name):
# Get the absolute path of the file/directory.
full_path = os.path.join(dir_name, file)
# Verify the path corresponds to a file, not a directory.
if os.path.isfile(full_path):
file_count += 1
return file_count
# Count the number of files in the parent directory of the current working
# directory.
parent_dir = os.path.dirname(os.getcwd())
print("File count in [", parent_dir, "]: ", count_files(parent_dir), sep="")
# Alternative version of the function using a list generator comprehension.
def count_files_2(input_dir):
"""Counts files present in the input directory."""
return sum(
(1 for x in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, x)))
)