{"id":1182646,"date":"2025-01-15T19:05:44","date_gmt":"2025-01-15T11:05:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1182646.html"},"modified":"2025-01-15T19:05:47","modified_gmt":"2025-01-15T11:05:47","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e5%ae%9a%e6%96%87%e4%bb%b6%e5%a4%a7%e5%b0%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1182646.html","title":{"rendered":"python\u5982\u4f55\u5224\u5b9a\u6587\u4ef6\u5927\u5c0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25130551\/c62a2300-91e4-494a-a047-6afd09728e47.webp\" alt=\"python\u5982\u4f55\u5224\u5b9a\u6587\u4ef6\u5927\u5c0f\" \/><\/p>\n<p><p> <strong>Python\u5224\u5b9a\u6587\u4ef6\u5927\u5c0f\u7684\u65b9\u6cd5\u6709\u591a\u79cd<\/strong>\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001Pathlib\u6a21\u5757\u3001shutil\u6a21\u5757\u7b49\u3002\u5176\u4e2d\uff0c<strong>os\u6a21\u5757\u662f\u6700\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u6cd5<\/strong>\uff0c\u56e0\u4e3a\u5b83\u5185\u7f6e\u4e8ePython\u6807\u51c6\u5e93\u4e2d\uff0c\u4e14\u529f\u80fd\u5f3a\u5927\u3001\u64cd\u4f5c\u7b80\u5355\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528os\u6a21\u5757\u5224\u5b9a\u6587\u4ef6\u5927\u5c0f\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cos\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u64cd\u4f5c\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u51fd\u6570\u3002\u8981\u5224\u5b9a\u6587\u4ef6\u5927\u5c0f\uff0c\u53ef\u4ee5\u4f7f\u7528os.path.getsize()\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u6587\u4ef6\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u6587\u4ef6\u7684\u5927\u5c0f\uff08\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\uff09\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>file_size = os.path.getsize(file_path)<\/p>\n<p>print(f&#39;The size of {file_path} is {file_size} bytes.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u6bb5\u5c06\u8f93\u51fa\u6307\u5b9a\u6587\u4ef6\u7684\u5927\u5c0f\u3002<strong>os.path.getsize()\u51fd\u6570\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u975e\u5e38\u76f4\u63a5\u548c\u9ad8\u6548<\/strong>\uff0c\u65e0\u9700\u6253\u5f00\u6587\u4ef6\uff0c\u4ec5\u901a\u8fc7\u6587\u4ef6\u8def\u5f84\u5373\u53ef\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001OS\u6a21\u5757\u5224\u5b9a\u6587\u4ef6\u5927\u5c0f<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002\u4f7f\u7528os.path.getsize()\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_file_size(file_path):<\/p>\n<p>    try:<\/p>\n<p>        size = os.path.getsize(file_path)<\/p>\n<p>        return size<\/p>\n<p>    except OSError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>file_path = &quot;example.txt&quot;<\/p>\n<p>file_size = get_file_size(file_path)<\/p>\n<p>if file_size is not None:<\/p>\n<p>    print(f&quot;The size of {file_path} is {file_size} bytes.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Could not determine the file size.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570get_file_size()\uff0c\u5b83\u63a5\u53d7\u6587\u4ef6\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u6587\u4ef6\u7684\u5927\u5c0f\u3002\u4f7f\u7528os.path.getsize()\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\u3002\u5982\u679c\u51fa\u73b0\u9519\u8bef\uff08\u4f8b\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\uff09\uff0c\u6211\u4eec\u6355\u83b7\u5f02\u5e38\u5e76\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u5927\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0cos.path.getsize()\u51fd\u6570\u4ecd\u7136\u9002\u7528\uff0c\u56e0\u4e3a\u5b83\u4e0d\u9700\u8981\u5c06\u6587\u4ef6\u5185\u5bb9\u8bfb\u53d6\u5230\u5185\u5b58\u4e2d\u3002\u5b83\u76f4\u63a5\u4ece\u6587\u4ef6\u7cfb\u7edf\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u4fe1\u606f\uff0c\u56e0\u6b64\u5373\u4f7f\u662f\u5904\u7406\u6570GB\u751a\u81f3TB\u7684\u5927\u6587\u4ef6\uff0c\u4e5f\u4e0d\u4f1a\u5bfc\u81f4\u5185\u5b58\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001Pathlib\u6a21\u5757\u5224\u5b9a\u6587\u4ef6\u5927\u5c0f<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>Pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\uff0c\u7528\u4e8e\u9762\u5411\u5bf9\u8c61\u5730\u64cd\u4f5c\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u4f7f\u7528Pathlib\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u76f4\u89c2\u5730\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def get_file_size(file_path):<\/p>\n<p>    try:<\/p>\n<p>        file = Path(file_path)<\/p>\n<p>        size = file.stat().st_size<\/p>\n<p>        return size<\/p>\n<p>    except FileNotFoundError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>file_path = &quot;example.txt&quot;<\/p>\n<p>file_size = get_file_size(file_path)<\/p>\n<p>if file_size is not None:<\/p>\n<p>    print(f&quot;The size of {file_path} is {file_size} bytes.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Could not determine the file size.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pathlib\u6a21\u5757\u7684Path\u7c7b\u6765\u8868\u793a\u6587\u4ef6\u8def\u5f84\u3002\u901a\u8fc7\u8c03\u7528stat()\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u7edf\u8ba1\u4fe1\u606f\uff0c\u5305\u62ec\u6587\u4ef6\u5927\u5c0f\uff08st_size\u5c5e\u6027\uff09\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u7b26\u53f7\u94fe\u63a5<\/h4>\n<\/p>\n<p><p>Pathlib\u6a21\u5757\u8fd8\u53ef\u4ee5\u5904\u7406\u7b26\u53f7\u94fe\u63a5\u3002\u5982\u679c\u6587\u4ef6\u662f\u4e00\u4e2a\u7b26\u53f7\u94fe\u63a5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528resolve()\u65b9\u6cd5\u83b7\u53d6\u5b9e\u9645\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u7136\u540e\u518d\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def get_file_size(file_path):<\/p>\n<p>    try:<\/p>\n<p>        file = Path(file_path).resolve()<\/p>\n<p>        size = file.stat().st_size<\/p>\n<p>        return size<\/p>\n<p>    except FileNotFoundError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>file_path = &quot;example.txt&quot;<\/p>\n<p>file_size = get_file_size(file_path)<\/p>\n<p>if file_size is not None:<\/p>\n<p>    print(f&quot;The size of {file_path} is {file_size} bytes.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Could not determine the file size.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001Shutil\u6a21\u5757\u5224\u5b9a\u6587\u4ef6\u5927\u5c0f<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u867d\u7136shutil\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u9ad8\u7ea7\u7684\u6587\u4ef6\u64cd\u4f5c\uff08\u5982\u590d\u5236\u3001\u79fb\u52a8\u6587\u4ef6\uff09\uff0c\u4f46\u5b83\u4e5f\u63d0\u4f9b\u4e86\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>def get_file_size(file_path):<\/p>\n<p>    try:<\/p>\n<p>        size = shutil.disk_usage(file_path).used<\/p>\n<p>        return size<\/p>\n<p>    except FileNotFoundError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>file_path = &quot;example.txt&quot;<\/p>\n<p>file_size = get_file_size(file_path)<\/p>\n<p>if file_size is not None:<\/p>\n<p>    print(f&quot;The size of {file_path} is {file_size} bytes.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Could not determine the file size.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528shutil\u6a21\u5757\u7684disk_usage()\u51fd\u6570\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5\uff0c\u5305\u62ec\u5df2\u7528\u7a7a\u95f4\u3001\u53ef\u7528\u7a7a\u95f4\u548c\u603b\u7a7a\u95f4\u3002\u901a\u8fc7\u8bbf\u95eeused\u5c5e\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u6587\u4ef6\u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>shutil\u6a21\u5757\u7684disk_usage()\u51fd\u6570\u8fd8\u53ef\u4ee5\u5904\u7406\u6574\u4e2a\u6587\u4ef6\u7cfb\u7edf\u7684\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u83b7\u53d6\u67d0\u4e2a\u76ee\u5f55\u7684\u603b\u5927\u5c0f\uff0c\u5305\u62ec\u5176\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\uff0c\u6211\u4eec\u53ef\u4ee5\u9012\u5f52\u904d\u5386\u76ee\u5f55\u5e76\u7d2f\u52a0\u6240\u6709\u6587\u4ef6\u7684\u5927\u5c0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_directory_size(directory_path):<\/p>\n<p>    total_size = 0<\/p>\n<p>    for dirpath, dirnames, filenames in os.walk(directory_path):<\/p>\n<p>        for filename in filenames:<\/p>\n<p>            file_path = os.path.join(dirpath, filename)<\/p>\n<p>            try:<\/p>\n<p>                total_size += os.path.getsize(file_path)<\/p>\n<p>            except OSError as e:<\/p>\n<p>                print(f&quot;Error: {e}&quot;)<\/p>\n<p>    return total_size<\/p>\n<p>directory_path = &quot;example_directory&quot;<\/p>\n<p>directory_size = get_directory_size(directory_path)<\/p>\n<p>print(f&quot;The total size of {directory_path} is {directory_size} bytes.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528os.walk()\u51fd\u6570\u9012\u5f52\u904d\u5386\u76ee\u5f55\uff0c\u5e76\u7d2f\u52a0\u6240\u6709\u6587\u4ef6\u7684\u5927\u5c0f\u3002\u6700\u7ec8\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u6574\u4e2a\u76ee\u5f55\u7684\u603b\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5224\u5b9a\u6587\u4ef6\u5927\u5c0f\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from pathlib import Path<\/p>\n<p>import shutil<\/p>\n<p>def get_file_size(file_path):<\/p>\n<p>    try:<\/p>\n<p>        # \u5c1d\u8bd5\u4f7f\u7528os\u6a21\u5757<\/p>\n<p>        size = os.path.getsize(file_path)<\/p>\n<p>        return size<\/p>\n<p>    except OSError:<\/p>\n<p>        pass<\/p>\n<p>    try:<\/p>\n<p>        # \u5c1d\u8bd5\u4f7f\u7528Pathlib\u6a21\u5757<\/p>\n<p>        file = Path(file_path)<\/p>\n<p>        size = file.stat().st_size<\/p>\n<p>        return size<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        pass<\/p>\n<p>    try:<\/p>\n<p>        # \u5c1d\u8bd5\u4f7f\u7528shutil\u6a21\u5757<\/p>\n<p>        size = shutil.disk_usage(file_path).used<\/p>\n<p>        return size<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        pass<\/p>\n<p>    return None<\/p>\n<p>file_path = &quot;example.txt&quot;<\/p>\n<p>file_size = get_file_size(file_path)<\/p>\n<p>if file_size is not None:<\/p>\n<p>    print(f&quot;The size of {file_path} is {file_size} bytes.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Could not determine the file size.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570get_file_size()\uff0c\u5b83\u4f9d\u6b21\u5c1d\u8bd5\u4f7f\u7528os\u6a21\u5757\u3001Pathlib\u6a21\u5757\u548cshutil\u6a21\u5757\u6765\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u3002\u5982\u679c\u6240\u6709\u65b9\u6cd5\u90fd\u5931\u8d25\uff0c\u5219\u8fd4\u56deNone\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u5e76\u786e\u4fdd\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u90fd\u80fd\u6b63\u786e\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u7279\u6b8a\u6587\u4ef6\u7c7b\u578b<\/h3>\n<\/p>\n<p><h4>1\u3001\u538b\u7f29\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u538b\u7f29\u6587\u4ef6\uff08\u5982ZIP\u3001TAR\u3001GZ\u7b49\uff09\uff0c\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u53ef\u80fd\u9700\u8981\u7279\u6b8a\u5904\u7406\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u6a21\u5757\uff08\u5982zipfile\u3001tarfile\u3001gzip\u7b49\uff09\u6765\u5904\u7406\u8fd9\u4e9b\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u83b7\u53d6ZIP\u6587\u4ef6\u5927\u5c0f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>def get_zip_file_size(zip_file_path):<\/p>\n<p>    try:<\/p>\n<p>        with zipfile.ZipFile(zip_file_path, &#39;r&#39;) as zip_file:<\/p>\n<p>            total_size = sum([zinfo.file_size for zinfo in zip_file.infolist()])<\/p>\n<p>        return total_size<\/p>\n<p>    except zipfile.BadZipFile as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>zip_file_path = &quot;example.zip&quot;<\/p>\n<p>zip_file_size = get_zip_file_size(zip_file_path)<\/p>\n<p>if zip_file_size is not None:<\/p>\n<p>    print(f&quot;The total size of files in {zip_file_path} is {zip_file_size} bytes.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Could not determine the ZIP file size.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528zipfile\u6a21\u5757\u6253\u5f00ZIP\u6587\u4ef6\uff0c\u5e76\u904d\u5386\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u6761\u76ee\uff0c\u7d2f\u52a0\u6bcf\u4e2a\u6587\u4ef6\u7684\u5927\u5c0f\uff0c\u6700\u7ec8\u5f97\u5230ZIP\u6587\u4ef6\u4e2d\u6240\u6709\u6587\u4ef6\u7684\u603b\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7f51\u7edc\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5b58\u50a8\u5728\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0a\u7684\u7f51\u7edc\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4f7f\u7528HTTP\u8bf7\u6c42\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528requests\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def get_remote_file_size(url):<\/p>\n<p>    try:<\/p>\n<p>        response = requests.head(url)<\/p>\n<p>        size = int(response.headers.get(&#39;Content-Length&#39;, 0))<\/p>\n<p>        return size<\/p>\n<p>    except requests.RequestException as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>url = &quot;https:\/\/example.com\/example.txt&quot;<\/p>\n<p>file_size = get_remote_file_size(url)<\/p>\n<p>if file_size is not None:<\/p>\n<p>    print(f&quot;The size of the remote file at {url} is {file_size} bytes.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Could not determine the remote file size.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528requests\u6a21\u5757\u53d1\u9001\u4e00\u4e2aHEAD\u8bf7\u6c42\u4ee5\u83b7\u53d6\u6587\u4ef6\u7684\u5934\u4fe1\u606f\uff0c\u5e76\u4ece\u54cd\u5e94\u5934\u4e2d\u63d0\u53d6Content-Length\u5b57\u6bb5\uff0c\u8be5\u5b57\u6bb5\u8868\u793a\u6587\u4ef6\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u8de8\u5e73\u53f0\u517c\u5bb9\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u91cf\u56e0\u7d20\u3002\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u53ef\u80fd\u6709\u4e0d\u540c\u7684\u6587\u4ef6\u7cfb\u7edf\u548c\u6587\u4ef6\u5c5e\u6027\uff0c\u56e0\u6b64\u5728\u7f16\u5199\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u7684\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u8003\u8651\u5230\u8fd9\u4e9b\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><h4>1\u3001Windows\u5e73\u53f0<\/h4>\n<\/p>\n<p><p>\u5728Windows\u5e73\u53f0\u4e0a\uff0cos.path.getsize()\u548cPathlib\u6a21\u5757\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u7279\u6b8a\u6587\u4ef6\uff08\u5982\u9690\u85cf\u6587\u4ef6\u3001\u7cfb\u7edf\u6587\u4ef6\uff09\uff0c\u53ef\u80fd\u9700\u8981\u989d\u5916\u7684\u6743\u9650\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u6743\u9650\u95ee\u9898\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_file_size(file_path):<\/p>\n<p>    try:<\/p>\n<p>        size = os.path.getsize(file_path)<\/p>\n<p>        return size<\/p>\n<p>    except PermissionError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>file_path = &quot;C:\\\\example.txt&quot;<\/p>\n<p>file_size = get_file_size(file_path)<\/p>\n<p>if file_size is not None:<\/p>\n<p>    print(f&quot;The size of {file_path} is {file_size} bytes.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Could not determine the file size.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6355\u83b7PermissionError\u5f02\u5e38\uff0c\u5e76\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u3002\u5982\u679c\u6ca1\u6709\u6743\u9650\u8bbf\u95ee\u6587\u4ef6\uff0c\u6211\u4eec\u5c06\u8fd4\u56deNone\u3002<\/p>\n<\/p>\n<p><h4>2\u3001Linux\u5e73\u53f0<\/h4>\n<\/p>\n<p><p>\u5728Linux\u5e73\u53f0\u4e0a\uff0c\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u7684\u65b9\u6cd5\u4e0eWindows\u7c7b\u4f3c\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u7279\u6b8a\u6587\u4ef6\uff08\u5982\u7b26\u53f7\u94fe\u63a5\u3001\u8bbe\u5907\u6587\u4ef6\uff09\uff0c\u53ef\u80fd\u9700\u8981\u7279\u6b8a\u5904\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_file_size(file_path):<\/p>\n<p>    try:<\/p>\n<p>        if os.path.islink(file_path):<\/p>\n<p>            file_path = os.readlink(file_path)<\/p>\n<p>        size = os.path.getsize(file_path)<\/p>\n<p>        return size<\/p>\n<p>    except OSError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>file_path = &quot;\/example.txt&quot;<\/p>\n<p>file_size = get_file_size(file_path)<\/p>\n<p>if file_size is not None:<\/p>\n<p>    print(f&quot;The size of {file_path} is {file_size} bytes.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Could not determine the file size.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u4e3a\u7b26\u53f7\u94fe\u63a5\uff0c\u5982\u679c\u662f\uff0c\u5219\u83b7\u53d6\u5b9e\u9645\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u7136\u540e\u518d\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u6216\u5927\u6587\u4ef6\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u91cf\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u6279\u91cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u83b7\u53d6\u591a\u4e2a\u6587\u4ef6\u7684\u5927\u5c0f\uff0c\u53ef\u4ee5\u6279\u91cf\u5904\u7406\u6587\u4ef6\uff0c\u4ee5\u51cf\u5c11I\/O\u64cd\u4f5c\u7684\u5f00\u9500\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_files_size(file_paths):<\/p>\n<p>    sizes = []<\/p>\n<p>    for file_path in file_paths:<\/p>\n<p>        try:<\/p>\n<p>            size = os.path.getsize(file_path)<\/p>\n<p>            sizes.append(size)<\/p>\n<p>        except OSError as e:<\/p>\n<p>            print(f&quot;Error: {e}&quot;)<\/p>\n<p>            sizes.append(None)<\/p>\n<p>    return sizes<\/p>\n<p>file_paths = [&quot;example1.txt&quot;, &quot;example2.txt&quot;, &quot;example3.txt&quot;]<\/p>\n<p>files_sizes = get_files_size(file_paths)<\/p>\n<p>for file_path, file_size in zip(file_paths, files_sizes):<\/p>\n<p>    if file_size is not None:<\/p>\n<p>        print(f&quot;The size of {file_path} is {file_size} bytes.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Could not determine the size of {file_path}.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570get_files_size()\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u5217\u8868\uff0c\u5e76\u8fd4\u56de\u6bcf\u4e2a\u6587\u4ef6\u7684\u5927\u5c0f\u3002\u901a\u8fc7\u6279\u91cf\u5904\u7406\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u51cf\u5c11I\/O\u64cd\u4f5c\u7684\u6b21\u6570\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5e76\u884c\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6280\u672f\u8fdb\u884c\u5e76\u884c\u5904\u7406\uff0c\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528concurrent.futures\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u591a\u7ebf\u7a0b\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def get_file_size(file_path):<\/p>\n<p>    try:<\/p>\n<p>        size = os.path.getsize(file_path)<\/p>\n<p>        return size<\/p>\n<p>    except OSError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>file_paths = [&quot;example1.txt&quot;, &quot;example2.txt&quot;, &quot;example3.txt&quot;]<\/p>\n<p>with ThreadPoolExecutor() as executor:<\/p>\n<p>    files_sizes = list(executor.map(get_file_size, file_paths))<\/p>\n<p>for file_path, file_size in zip(file_paths, files_sizes):<\/p>\n<p>    if file_size is not None:<\/p>\n<p>        print(f&quot;The size of {file_path} is {file_size} bytes.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Could not determine the size of {file_path}.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528ThreadPoolExecutor\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u5e76\u901a\u8fc7executor.map()\u65b9\u6cd5\u5e76\u884c\u5904\u7406\u6587\u4ef6\u5927\u5c0f\u7684\u83b7\u53d6\u64cd\u4f5c\u3002\u8fd9\u6837\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5904\u7406\u5927\u91cf\u6587\u4ef6\u65f6\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9519\u8bef\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\u662f\u4fdd\u8bc1\u4ee3\u7801\u5065\u58ee\u6027\u7684\u91cd\u8981\u624b\u6bb5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5728\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\uff08\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u3001\u6587\u4ef6\u7cfb\u7edf\u9519\u8bef\u7b49\uff09\u3002\u6211\u4eec\u5e94\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4e3a\u5f02\u5e38\u800c\u5d29\u6e83\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def get_file_size(file_path):<\/p>\n<p>    try:<\/p>\n<p>        size = os.path.getsize(file_path)<\/p>\n<p>        return size<\/p>\n<p>    except FileNotFoundError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>    except PermissionError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>    except OSError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>file_path = &quot;example.txt&quot;<\/p>\n<p>file_size = get_file_size(file_path)<\/p>\n<p>if file_size is not None:<\/p>\n<p>    print(f&quot;The size of {file_path} is {file_size} bytes.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Could not determine the file size.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5206\u522b\u6355\u83b7FileNotFoundError\u3001PermissionError\u548cOSError\u5f02\u5e38\uff0c\u5e76\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684logging\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bb0\u5f55\u65e5\u5fd7\u4fe1\u606f\uff0c\u5305\u62ec\u9519\u8bef\u4fe1\u606f\u3001\u8c03\u8bd5\u4fe1\u606f\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import logging<\/p>\n<p>logging.basicConfig(level=logging.INFO, format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>def get_file_size(file_path):<\/p>\n<p>    try:<\/p>\n<p>        size = os.path.getsize(file_path)<\/p>\n<p>        logging.info(f&quot;File size of {file_path}: {size} bytes&quot;)<\/p>\n<p>        return size<\/p>\n<p>    except FileNotFoundError as e:<\/p>\n<p>        logging.error(f&quot;File not found: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>    except PermissionError as e:<\/p>\n<p>        logging.error(f&quot;Permission denied: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>    except OSError as e:<\/p>\n<p>        logging.error(f&quot;OS error: {e}&quot;)<\/p>\n<p>        return None<\/p>\n<p>file_path = &quot;example.txt&quot;<\/p>\n<p>file_size = get_file_size(file_path)<\/p>\n<p>if file_size is not None:<\/p>\n<p>    print(f&quot;The size of {file_path} is {file_size} bytes.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Could not determine the file size.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528logging\u6a21\u5757\u8bb0\u5f55\u65e5\u5fd7\u4fe1\u606f\uff0c\u5305\u62ec\u6587\u4ef6\u5927\u5c0f\u548c\u9519\u8bef\u4fe1\u606f\u3002\u901a\u8fc7\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u548c\u683c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u65e5\u5fd7\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5224\u5b9a\u6587\u4ef6\u5927\u5c0f\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<strong>os\u6a21\u5757\u3001Pathlib\u6a21\u5757\u548cshutil\u6a21\u5757\u90fd\u662f\u5e38\u7528\u7684\u5de5\u5177<\/strong>\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c<strong>\u5904\u7406\u7279\u6b8a\u6587\u4ef6\u7c7b\u578b\u3001\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u3001\u6027\u80fd\u4f18\u5316\u548c\u9519\u8bef\u5904\u7406\u90fd\u662f\u9700\u8981\u8003\u8651\u7684\u91cd\u8981\u56e0\u7d20<\/strong>\u3002\u901a\u8fc7\u5408\u7406\u5730\u8bbe\u8ba1\u548c\u4f18\u5316\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u3001\u51c6\u786e\u5730\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\uff0c\u5e76\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c<strong>\u65e5\u5fd7\u8bb0\u5f55\u662f\u4fdd\u8bc1\u4ee3\u7801\u5065\u58ee\u6027\u7684\u91cd\u8981\u624b\u6bb5<\/strong>\u3002\u4f7f\u7528Python\u7684logging\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bb0\u5f55\u548c\u7ba1\u7406\u65e5\u5fd7\u4fe1\u606f\uff0c\u5e2e\u52a9\u6211\u4eec\u5728\u5f00\u53d1\u548c\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\u5feb\u901f\u5b9a\u4f4d\u548c\u89e3\u51b3\u95ee\u9898\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u7406\u89e3\u548c\u638c\u63e1Python\u5224\u5b9a\u6587\u4ef6\u5927\u5c0f\u7684\u65b9\u6cd5\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>os<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u4f7f\u7528<code>os.path.getsize()<\/code>\u51fd\u6570\uff0c\u4f20\u5165\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u8fd4\u56de\u503c\u662f\u6587\u4ef6\u7684\u5b57\u8282\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nfile_size = os.path.getsize(&#39;your_file.txt&#39;)\nprint(f&#39;\u6587\u4ef6\u5927\u5c0f\u4e3a: {file_size} \u5b57\u8282&#39;)\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Python\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u4e3a\u7a7a\uff1f<\/strong><br \/>\u8981\u5224\u65ad\u6587\u4ef6\u662f\u5426\u4e3a\u7a7a\uff0c\u53ef\u4ee5\u5728\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\u4e4b\u540e\u8fdb\u884c\u5224\u65ad\u3002\u5982\u679c\u6587\u4ef6\u5927\u5c0f\u4e3a0\uff0c\u5219\u8bf4\u660e\u6587\u4ef6\u4e3a\u7a7a\u3002\u4f7f\u7528<code>os.path.getsize()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">if os.path.getsize(&#39;your_file.txt&#39;) == 0:\n    print(&#39;\u6587\u4ef6\u4e3a\u7a7a&#39;)\nelse:\n    print(&#39;\u6587\u4ef6\u4e0d\u4e3a\u7a7a&#39;)\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u5982\u4f55\u5904\u7406\u5927\u6587\u4ef6\u7684\u5927\u5c0f\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.path.getsize()<\/code>\u540c\u6837\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f\uff0c\u4f46\u4e3a\u4e86\u907f\u514d\u5185\u5b58\u4e0d\u8db3\u7684\u95ee\u9898\uff0c\u5efa\u8bae\u4f7f\u7528\u6587\u4ef6\u6d41\u8bfb\u53d6\u7684\u65b9\u5f0f\u6765\u5206\u5757\u5904\u7406\u6587\u4ef6\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff0c\u4ee5\u6b64\u6765\u6709\u6548\u7ba1\u7406\u5185\u5b58\u5e76\u5904\u7406\u5927\u6587\u4ef6\u3002\u4f7f\u7528<code>with open()<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u540e\u6b63\u786e\u5173\u95ed\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5224\u5b9a\u6587\u4ef6\u5927\u5c0f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001Pathlib\u6a21\u5757\u3001shutil\u6a21\u5757\u7b49\u3002\u5176\u4e2d\uff0cos\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1182658,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182646"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1182646"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182646\/revisions"}],"predecessor-version":[{"id":1182662,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182646\/revisions\/1182662"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1182658"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1182646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1182646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1182646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}