forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls_v3.py
More file actions
31 lines (21 loc) · 710 Bytes
/
Copy pathls_v3.py
File metadata and controls
31 lines (21 loc) · 710 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
import argparse
import datetime
from pathlib import Path
parser = argparse.ArgumentParser(prog="ls")
parser.add_argument("path")
parser.add_argument("-l", "--long", action="store_true")
args = parser.parse_args()
target_dir = Path(args.path)
if not target_dir.exists():
print("The target directory doesn't exist")
raise SystemExit(1)
def build_output(entry, long=False):
if long:
size = entry.stat().st_size
date = datetime.datetime.fromtimestamp(entry.stat().st_mtime).strftime(
"%b %d %H:%M:%S"
)
return f"{size:>6d} {date} {entry.name}"
return entry.name
for entry in target_dir.iterdir():
print(build_output(entry, long=args.long))