-
Notifications
You must be signed in to change notification settings - Fork 0
/
concat.py
64 lines (52 loc) · 1.68 KB
/
concat.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
import sys
def concat(filename):
"""
This function concatenates and displays one or more files
whose names are provided as command line parameters.
@param: Strings
@return: None
"""
# Initialize variables.
ls = list()
bad_files = list()
# Go through the file names in the list of arguments.
for file in filename:
# Check for error while processing.
try:
# Convert the contents of file to list and append to ls.
with open(file) as f:
ls.extend(f.readlines())
ls.append('\n')
except FileNotFoundError:
# Add invalid names to bad_files.
bad_files.append(file)
if len(ls) > 0:
# Open a new file and write the contents of ls into it.
with open('new_file.txt', 'w') as new_file:
for line in ls:
new_file.write(line)
# Display the contents of new file.
with open('new_file.txt', 'r') as f:
for l in f:
print(l.rstrip())
# Return invalid names as list, bad_files.
return bad_files
def main():
# Make sure user provided at least one name to be processed.
if len(sys.argv) < 2:
print("Please, provide a file name!")
else:
# Concatenate and print the contents of the provide file names
# and return the invalid ones.
filename = sys.argv[1:]
bad_files_names = concat(filename)
print()
if len(sys.argv) > 1:
i = 1
# Print the invalid names.
print("Invalid Entries:")
for name in bad_files_names:
print(f'{i}: {name}')
i += 1
if __name__ == "__main__":
main()