-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove os import and add function to get file size
- Loading branch information
Showing
2 changed files
with
42 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,41 @@ | ||
import os | ||
from pathlib import Path, PurePath | ||
import shutil | ||
|
||
|
||
def get_file_from_path(file_path): | ||
return os.path.basename(file_path) | ||
return PurePath(file_path).name | ||
|
||
def split_file_and_extension(file): | ||
return file.rpartition(".")[0], file.rpartition(".")[-1] | ||
def get_file_name_without_extension(file): | ||
return file.rpartition(".")[0] | ||
|
||
def create_folders(): | ||
if not os.path.exists("database/"): | ||
os.makedirs("database/") | ||
if not os.path.exists("downloads/"): | ||
os.makedirs("downloads/") | ||
if not Path.exists(Path("database/")): | ||
Path.mkdir(Path("database/")) | ||
|
||
def create_temp_folder(): | ||
if not os.path.exists("temp/"): | ||
os.makedirs("temp/") | ||
if not Path.exists(Path("temp/")): | ||
Path.mkdir(Path("temp/")) | ||
|
||
def delete_temp_folder(): | ||
if os.path.exists("temp/"): | ||
shutil.rmtree("temp/") | ||
if Path.exists(Path("temp/")): | ||
shutil.rmtree("temp/") | ||
|
||
def get_file_size(file_path): | ||
return _convert_size(Path(file_path).stat().st_size) | ||
|
||
def _convert_size(size_bytes): | ||
# Edge case for size 0 bytes | ||
if size_bytes == 0: | ||
return "0 B" | ||
|
||
# List of size units | ||
size_units = ["B", "KB", "MB", "GB"] | ||
i = 0 | ||
|
||
# Convert to larger units until size is below 1024 | ||
while size_bytes >= 1024 and i < len(size_units) - 1: | ||
size_bytes /= 1024 | ||
i += 1 | ||
|
||
# Return formatted size with 2 decimal places | ||
return f"{size_bytes:.2f} {size_units[i]}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters