forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls_v4.py
More file actions
35 lines (25 loc) · 812 Bytes
/
Copy pathls_v4.py
File metadata and controls
35 lines (25 loc) · 812 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
import argparse
import datetime
from pathlib import Path
parser = argparse.ArgumentParser(
prog="ls",
description="List the content of a directory",
epilog="Thanks for using %(prog)s! :)",
)
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))