Update precompile.md

This commit is contained in:
blueloveTH 2024-04-14 16:26:39 +08:00
parent ecf49321e3
commit e4755101bc

View File

@ -123,14 +123,10 @@ You can use this snnipet to convert every python file in a directory into precom
```python ```python
# precompile.py # precompile.py
import sys import sys, os
import os
count = 0
def precompile(filepath: str): def precompile(filepath: str):
global count """Precompile a python file inplace"""
++count
with open(filepath, 'r') as f: with open(filepath, 'r') as f:
source = f.read() source = f.read()
source = compile(source, filepath, 'exec') source = compile(source, filepath, 'exec')
@ -138,16 +134,11 @@ def precompile(filepath: str):
f.write(source) f.write(source)
def traverse(root: str): def traverse(root: str):
"""Traverse a directory and precompile every python file"""
for entry in os.listdir(root): for entry in os.listdir(root):
entrypath = os.path.join(root, entry) entrypath = os.path.join(root, entry)
if os.path.isdir(entrypath): if os.path.isdir(entrypath):
traverse(entrypath) traverse(entrypath)
elif entrypath.endswith(".py"): elif entrypath.endswith(".py"):
precompile(entrypath) precompile(entrypath)
traverse(sys.argv[2])
``` ```
```bash
pkpy.exe precompile.py ./path/to/directory
```