-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversions.py
More file actions
40 lines (31 loc) · 1.05 KB
/
versions.py
File metadata and controls
40 lines (31 loc) · 1.05 KB
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
38
39
40
# versions.py
import platform
import pkg_resources
import sys
# Define the output file (if redirected)
output_file = "versions.txt"
# Custom class to write to both stdout and a file
class Tee:
def __init__(self, stdout, file):
self.stdout = stdout
self.file = file
def write(self, text):
self.stdout.write(text)
self.file.write(text)
def flush(self):
self.stdout.flush()
self.file.flush()
# Open the output file and set up dual output
with open(output_file, 'w') as f:
# Redirect stdout to both terminal and file
sys.stdout = Tee(sys.__stdout__, f)
# Your original code
print("============================")
print(f"Python Version: {platform.python_version()}")
print("============================")
print("Redirect to text file or stdout")
print("Installed Package Versions:")
for dist in sorted(pkg_resources.working_set, key=lambda d: d.project_name.lower()):
print(f"{dist.project_name}=={dist.version}")
# Restore original stdout
sys.stdout = sys.__stdout__