| 12345678910111213141516171819202122232425262728293031 |
- import os
- import re
- from datetime import datetime
- from pathlib import Path
- MAX_AGE = datetime.now().timestamp() - 12 * 60 * 60
- def csv_trim(dirname: str = "misc/data"):
- if Path(dirname).is_file():
- csv_trim_file(Path(dirname), True)
- else:
- for csv_file in Path(dirname).glob("*.csv"):
- csv_trim_file(csv_file)
- def csv_trim_file(csv_file: Path, ignore_age: bool = False):
- temp_file = Path(str(csv_file) + ".tmp")
- file_mtime = csv_file.stat().st_mtime
- if not ignore_age and file_mtime < MAX_AGE:
- return
- print(csv_file.name)
- with open(csv_file, "r", encoding="latin-1", errors="ignore") as frh:
- with open(temp_file, "w", encoding="latin-1") as fwh:
- for line in frh.readlines():
- fwh.write(re.sub(r"[ ]+\t", "\t", line))
- os.utime(temp_file, (file_mtime, file_mtime))
- csv_file.unlink()
- temp_file.rename(csv_file)
|