-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonCat.py
More file actions
128 lines (93 loc) · 3.61 KB
/
pythonCat.py
File metadata and controls
128 lines (93 loc) · 3.61 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""Concatenates the three largest files in a directory, saving them to file."""
import os
# Write a Python function
# that takes a directory as an argument
# finds the 3 biggest files in that directory
# and concatenates those files together into a new file
class File:
"""Object representing some operating system file."""
def __init__(self, filename, fileSize, fileContents):
"""Initialize a File object.
Args:
filename: the name of a file.
fileSize: the size of the file.
fileContents: the contents of the file.
"""
self.filename = filename
self.fileSize = fileSize
self.fileContents = fileContents
def getFilename(self):
"""Return the object's filename.
Returns:
The object's filename.
"""
return self.filename
def getFileSize(self):
"""Return the object's filesize.
Returns:
The object's file size.
"""
return self.fileSize
def getFileContents(self):
"""Return the object's contents.
Returns:
The object's contents.
"""
return self.fileContents
def printFileNameAndSize(self):
"""Print the file's filename and file size."""
filename = self.getFilename()
fileSize = self.getFileSize()
print("{0:<32.32} {1:>8}".format(filename, fileSize))
def pythonCat(someDirectory):
"""
Concatenates the three largest files in a directory, saving them to file.
Python Concatenater (pythonCat) searches a given directory for the three
largest files and concatenates their contents into a new file. This new
file is called newFile.txt by default.
Currently, the program only works on text files that Python can easily
convert into strings. This should be fixed in the future.
Args:
someDirectory: any given directory.
Raises:
UnicodeDecodeError: if a file contains characters that can't be Unicode
decoded, the issue is caught and that file is skipped.
"""
files = []
# Do not include this program or any files this program creates.
invalidFiles = ["pythonCat.py", "newFile.txt"]
for systemFile in os.listdir(someDirectory):
if os.path.isfile(systemFile) and systemFile not in invalidFiles:
try:
filename = systemFile
fileSize = os.stat(systemFile).st_size
with open(systemFile, 'r') as openSystemFile:
fileContents = openSystemFile.read()
files.append(File(filename, fileSize, fileContents))
except UnicodeDecodeError:
print("File: {} contains characters that the 'ascii' codec "
"can't decode. Skipping.".format(filename))
continue
generateFileTable(files)
files.sort(key=lambda x: x.getFileSize(), reverse=True)
print("\n\nFILES SORTED BY SIZE IN DESCENDING ORDER")
generateFileTable(files)
concatenatedString = ""
for i in range(0, 3):
concatenatedString += files[i].getFileContents()
with open("newFile.txt", 'w') as newFile:
newFile.write(concatenatedString)
print("\nnewFile.txt created from three largest files.")
def generateFileTable(listOfFiles):
"""Print a table showing information on a list of SystemFile objects.
Args:
listOfFiles: any list of File objects.
"""
print("{0:<32} {1:>8}".format("FILE", "SIZE"))
for item in listOfFiles:
item.printFileNameAndSize()
def main():
"""Test pythonCat."""
pythonCat(".")
if __name__ == "__main__":
main()