Skip to content

Commit f772fb8

Browse files
author
sambitr
committed
step-4 and all scenarios convered. Solution achieved
1 parent 05438d3 commit f772fb8

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed

Coding Challenge#33[linux head]/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,40 @@ Hello, World
103103

104104
C:\Users\kumar\Documents\Python Scripts\Coding-Challenges\Coding Challenge#33[linux head]>
105105
<br>########## ==================<br>
106+
107+
C:\Users\kumar\Documents\Python Scripts\Coding-Challenges\Coding Challenge#33[linux head]>python head.py -n 10 text.txt text2.txt
108+
==> text.txt <==
109+
The Project Gutenberg eBook of The Art of War
110+
111+
112+
113+
This ebook is for the use of anyone anywhere in the United States and
114+
115+
most other parts of the world at no cost and with almost no restrictions
116+
117+
whatsoever. You may copy it, give it away or re-use it under the terms
118+
119+
of the Project Gutenberg License included with this ebook or online
120+
121+
at www.gutenberg.org. If you are not located in the United States,
122+
123+
you will have to check the laws of the country where you are located
124+
125+
before using this eBook.
126+
127+
128+
129+
==> text2.txt <==
130+
Hello, World
131+
132+
133+
134+
135+
136+
137+
138+
139+
140+
141+
C:\Users\kumar\Documents\Python Scripts\Coding-Challenges\Coding Challenge#33[linux head]>python head.py -n 10 text.txt
142+
<br>########## ================== <br>

Coding Challenge#33[linux head]/head.py

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import sys
22

3-
#print(sys.argv[0], sys.argv[1], sys.argv[2], sys.argv[3])
43
if len(sys.argv) == 2: ## first item is the script name and second item is the file name sent as argument
5-
## take the file name from the command line and print the contents of it
64
file_name = sys.argv[1]
7-
#print(f"file name is: {file_name}")
85

96
file_handle = open(file_name, 'r', encoding="utf-8")
107
print(file_handle.read())
118
file_handle.close()
12-
######################
139
else:
1410
## no file name. Take option from command line and print it. Shold exit ater 10 lines.
1511
if len(sys.argv) == 1 :
@@ -20,13 +16,27 @@
2016

2117
elif sys.argv[1] == "-n":
2218
line_to_print = sys.argv[2]
23-
file_name_to_work = sys.argv[3]
24-
#file_operation(file_name_to_work, sys.argv[1], line_to_print)
25-
#print(f"line to print {line_to_print}, file to work {file_name_to_work}")
26-
file_hdl = open(file_name_to_work, 'r', encoding="utf-8")
27-
for i in range(int(line_to_print)):
28-
print(file_hdl.readline())
29-
file_hdl.close()
19+
files = sys.argv[3:]
20+
if len(files) > 1:
21+
for fileName in files:
22+
print(f"==> {fileName} <==")
23+
filehdl = open(fileName, 'r', encoding="utf-8")
24+
for i in range(int(line_to_print)):
25+
line = filehdl.readline()
26+
if line == "": ## end of file has been reached
27+
break
28+
print(line)
29+
filehdl.close()
30+
else:
31+
file_name_to_work = sys.argv[3]
32+
file_hdl = open(file_name_to_work, 'r', encoding="utf-8")
33+
for i in range(int(line_to_print)):
34+
line = file_hdl.readline()
35+
if line == "": ## end of file has been reached
36+
break
37+
print(line)
38+
39+
file_hdl.close()
3040

3141
elif sys.argv[1] == "-c":
3242
chars_to_print = sys.argv[2]

0 commit comments

Comments
 (0)