-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython
More file actions
38 lines (32 loc) · 883 Bytes
/
Python
File metadata and controls
38 lines (32 loc) · 883 Bytes
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
##### Convert Kilometers to Miles
# Global constant for conversion
KILOMETERS_TO_MILES = 0.6214
# main def
def main():
# Get distance in kilometers
Kilometer=float(input('enter a distance in kilometers: '))
KILOMETERS_TO_MILES = 0.6214
Miles = Kilometer * KILOMETERS_TO_MILES
print(Miles, 'miles')
# Call the main function.
main()
##### Display First 5 lines of a file
def main():
counter = 0
file_name = input('Enter the name of the file: ')
open_file = open(file_name, 'r')
for _ in open_file:
counter += 1
open_file.close()
open_file = open(file_name, 'r')
if counter < 5:
for idx in open_file:
idx = idx[:-1]
print(idx)
else:
first_five = (open_file.readlines())[:5]
for j in first_five:
j = j[:-1]
print(j)
open_file.close()
main()