Download src/utils/file_ops.py from amkyawdev/burme-coder-max: direct link, hf CLI and curl.
- Browser
- Download file 4.07 kB
-
https://huggingface.co/datasets/amkyawdev/burme-coder-max/resolve/main/src/utils/file_ops.py
- Command line
-
hf download hf://datasets/amkyawdev/burme-coder-max/src/utils/file_ops.py
-
curl -L -o file_ops.py https://huggingface.co/datasets/amkyawdev/burme-coder-max/resolve/main/src/utils/file_ops.py
4.07 kB
| """File Operations Utility""" | |
| import os | |
| import shutil | |
| import json | |
| from pathlib import Path | |
| from typing import List, Optional, Dict, Any | |
| from datetime import datetime | |
| class FileOps: | |
| """File operation utilities""" | |
| def read_text(file_path: str, encoding: str = "utf-8") -> str: | |
| """Read text content from file""" | |
| with open(file_path, "r", encoding=encoding) as f: | |
| return f.read() | |
| def write_text(file_path: str, content: str, encoding: str = "utf-8"): | |
| """Write text content to file""" | |
| Path(file_path).parent.mkdir(parents=True, exist_ok=True) | |
| with open(file_path, "w", encoding=encoding) as f: | |
| f.write(content) | |
| def read_json(file_path: str) -> Dict[str, Any]: | |
| """Read JSON file""" | |
| with open(file_path, "r", encoding="utf-8") as f: | |
| return json.load(f) | |
| def write_json(file_path: str, data: Dict[str, Any], indent: int = 2): | |
| """Write JSON file""" | |
| Path(file_path).parent.mkdir(parents=True, exist_ok=True) | |
| with open(file_path, "w", encoding="utf-8") as f: | |
| json.dump(data, f, indent=indent, ensure_ascii=False) | |
| def append_jsonl(file_path: str, data: Dict[str, Any]): | |
| """Append data as JSONL line""" | |
| Path(file_path).parent.mkdir(parents=True, exist_ok=True) | |
| with open(file_path, "a", encoding="utf-8") as f: | |
| f.write(json.dumps(data, ensure_ascii=False) + "\n") | |
| def read_jsonl(file_path: str) -> List[Dict[str, Any]]: | |
| """Read JSONL file""" | |
| results = [] | |
| with open(file_path, "r", encoding="utf-8") as f: | |
| for line in f: | |
| if line.strip(): | |
| results.append(json.loads(line)) | |
| return results | |
| def list_files(directory: str, pattern: str = "*") -> List[Path]: | |
| """List files in directory matching pattern""" | |
| return list(Path(directory).glob(pattern)) | |
| def create_directory(path: str): | |
| """Create directory if it doesn't exist""" | |
| Path(path).mkdir(parents=True, exist_ok=True) | |
| def delete_file(path: str): | |
| """Delete file""" | |
| Path(path).unlink(missing_ok=True) | |
| def copy_file(src: str, dst: str): | |
| """Copy file""" | |
| Path(dst).parent.mkdir(parents=True, exist_ok=True) | |
| shutil.copy2(src, dst) | |
| def move_file(src: str, dst: str): | |
| """Move file""" | |
| Path(dst).parent.mkdir(parents=True, exist_ok=True) | |
| shutil.move(src, dst) | |
| def get_file_info(path: str) -> Dict[str, Any]: | |
| """Get file information""" | |
| p = Path(path) | |
| stat = p.stat() | |
| return { | |
| "path": str(p.absolute()), | |
| "name": p.name, | |
| "size": stat.st_size, | |
| "modified": datetime.fromtimestamp(stat.st_mtime).isoformat(), | |
| "is_file": p.is_file(), | |
| "is_dir": p.is_dir(), | |
| } | |
| def find_files(directory: str, extensions: List[str]) -> List[Path]: | |
| """Find files with specific extensions""" | |
| results = [] | |
| for ext in extensions: | |
| results.extend(Path(directory).rglob(f"*.{ext}")) | |
| return results | |
| def get_directory_size(path: str) -> int: | |
| """Calculate directory size in bytes""" | |
| total = 0 | |
| for p in Path(path).rglob("*"): | |
| if p.is_file(): | |
| total += p.stat().st_size | |
| return total | |
| def clean_directory(path: str, keep_pattern: Optional[str] = None): | |
| """Clean directory, optionally keeping files matching pattern""" | |
| for item in Path(path).iterdir(): | |
| if keep_pattern and item.match(keep_pattern): | |
| continue | |
| if item.is_file(): | |
| item.unlink() | |
| elif item.is_dir(): | |
| shutil.rmtree(item) | |