-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyFilePathCalculator.py
More file actions
60 lines (47 loc) · 1.74 KB
/
pyFilePathCalculator.py
File metadata and controls
60 lines (47 loc) · 1.74 KB
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
'''
Title: Recursive Length of File 0.7.1
Author: Joe Friedrich
License: MIT
'''
import openpyxl
import os
import datetime
def get_excel_output_location():
'''
Write!
'''
print('\nThis will save a file in .xlsx format to your desktop.')
print("It's name will be the current time.xlsx.")
user_folder = os.environ['USERPROFILE']
time = datetime.datetime.now()
time = time.strftime("%Y-%m-%d.%H.%M.%S")
file_location = user_folder + "\desktop\py" + time + ".xlsx"
return file_location
def get_starting_directory():
'''
Write!
'''
print('\nPlease enter the top level of your path.')
starting_directory = input() #get starting directory from user
return starting_directory
def load_directory_tree(start_walking):
'''
Write!
'''
directory_tree = os.walk(start_walking)
return directory_tree
#------------------Begin Program------------------------
print("\nWelcome to the Recursive 'Length of file-path' program.")
file_location = get_excel_output_location()
excel_file = openpyxl.Workbook() #create a new excel workbook
starting_directory = get_starting_directory()
directory_tree = load_directory_tree(starting_directory)
worksheet = excel_file.active #use the first sheet of the workbook
worksheet.append(['directory', 'file', 'length', starting_directory]) #sets title line
for directory in directory_tree: #traverses directories
for file in directory[2]: #for all files in the directory
length_of_path = len(directory[0]) + 1 + len(file) - len(starting_directory) #calculate length
worksheet.append([directory[0],file,length_of_path]) #write the directory/file/length
excel_file.save(file_location)
print('\nExcel file saved: ' + file_location)
print('\nThank you and have a nice day.\n')