forked from kal179/Beginners_Python_Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadFiles.py
More file actions
34 lines (31 loc) · 939 Bytes
/
Copy pathreadFiles.py
File metadata and controls
34 lines (31 loc) · 939 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
# Reading a file in a directory
from pathlib import Path
#check if valid directory
while True:
try:
d = str(input('Please enter a directory path: '))
d_path = Path(d)
if d_path.is_dir():
break
else:
raise ValueError
except (ValueError, OSError):
print('Specified directory path not found')
#show all files in directory chosen
lst = []
for obj in d_path.iterdir():
if obj.is_file():
lst.append(obj)
# sorting lexicographically taking each path object as a string
lst_copy = sorted(lst, key=lambda obj: str(obj))
for obj in lst_copy:
print(obj)
#open file
while True:
try:
file = str(input('Please enter a file name: '))
for line in open(file): #more memory efficient
line = line.rstrip('\n')
print(line)
except (OSError, FileNotFoundError):
print('File error')