LK CS BookHomeHomePythonPythonFrontendFrontendShellShell
GitHubGitHub (opens in a new tab)
      • Horizontal Scroll
      • Reactangle Radius Clip
    • Auto Wrap in Treewiget Cell
  • Get File Hash
  • Get File Size
  • Get Func Source
  • Load Module by Path
  • Pip Commonly Used
  • Poetry Memo
  • Streamlit
  • Text Parens Scanner
  • Watch File Changes
  • Xlsxwriter
  • Zipfiles
  • Home
        • Horizontal Scroll
        • Reactangle Radius Clip
      • Auto Wrap in Treewiget Cell
    • Get File Hash
    • Get File Size
    • Get Func Source
    • Load Module by Path
    • Pip Commonly Used
    • Poetry Memo
    • Streamlit
    • Text Parens Scanner
    • Watch File Changes
    • Xlsxwriter
    • Zipfiles
    • Mdx Embed Video
    • Mdx Syntax Highlighting for Pycharm
    • Bore Frp
    • Chrome App Mode
    • Ssh Keygen
      • Create Relative Path Lnk File
Question? Give us feedback → (opens in a new tab)Edit this page
Python
Get File Size

Get File Size

import os
 
def get_file_size(filepath: str) -> int:
    return os.path.getsize(filepath)
 
def get_file_size_str(filepath: str) -> str:
    size = float(os.path.getsize(filepath))
    for unit in ('B', 'KB', 'MB', 'GB', 'TB'):
        if size < 1024:
            return f'{size:.2f}{unit}'
        size /= 1024
    return f'{size:.2f}PB'
Get File HashGet Func Source

Made by Likiata, powered by Nextra