-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistfileext.py
70 lines (52 loc) · 1.22 KB
/
listfileext.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
65
66
67
68
69
70
#!/usr/bin/env python
#
# Imports
#
import os
import sys
#
# Setup
#
skip_hidden_items = True
file_extensions = []
# Get current directory
# @see https://docs.python.org/2/tutorial/stdlib.html#operating-system-interface
path = os.getcwd()
#
# User input
#
# Get command line arguments
# @see https://docs.python.org/2/tutorial/stdlib.html#command-line-arguments
user_input = sys.argv
# Check user input
if ( len(user_input) >= 2 ) :
if ( user_input[1].index("/") == 0 ):
path = user_input[1]
else:
print("Error: Not a correct path, it has to start with a slash '/'")
exit()
#
# Search for files
#
def get_items(path):
items = os.listdir(path)
for item in items:
item_path = path + "/" + item
if not ( skip_hidden_items and item.startswith( "." ) ):
# Check if item is file or directory and search recursivly
if ( os.path.isfile(item_path) ):
# Get file extension
extension = item[item.rfind("."):]
# Check if filee extension is already listet or add
if ( extension not in file_extensions ):
file_extensions.append(extension)
else:
get_items(item_path)
# Start searching
get_items(path)
#
# Print file types
#
file_extensions.sort()
for extension in file_extensions:
print(extension)