diff options
Diffstat (limited to 'file_ops.py')
-rw-r--r-- | file_ops.py | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/file_ops.py b/file_ops.py index bbad813..8c7178d 100644 --- a/file_ops.py +++ b/file_ops.py @@ -1,6 +1,6 @@ # my functions for file creation/deletion operations # also for file path string stuff -import os, shutil +import os, shutil, io # function to format a path string correctly: # - no duplicate slashes @@ -10,7 +10,7 @@ def get_path_str(path): # check params if (type(path) != str): - return "" + return None # else work on the string rtn_str = "" @@ -233,12 +233,19 @@ def rm_folder(path): return True # function to get the size of a file -def get_file_size(path): - - # check params - if (is_file(path) == False): - return 0 - return os.path.getsize(get_path_str(path)) +def get_file_size(path_or_stream): + # check params and get the size if the + # thing is a file or a stream object + if (type(path_or_stream) == str and is_file(path_or_stream) == True): + return os.path.getsize(get_path_str(path_or_stream)) + elif (type(path_or_stream) == io.BytesIO): + size = 0 + path_or_stream.seek(0) + while (path_or_stream.read(1) != b""): + size += 1 + path_or_stream.seek(0) + return size + return 0 # function to rename a folder or a file def rename(current_name, new_name): |