{"id":1019678,"date":"2024-12-27T13:03:12","date_gmt":"2024-12-27T05:03:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1019678.html"},"modified":"2024-12-27T13:03:17","modified_gmt":"2024-12-27T05:03:17","slug":"python%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85os%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1019678.html","title":{"rendered":"python\u5982\u4f55\u5b89\u88c5os\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25162125\/a800bd7c-02c4-4d8a-951c-133fe0be448d.webp\" alt=\"python\u5982\u4f55\u5b89\u88c5os\u5e93\" \/><\/p>\n<p><p> <strong>Python\u4e0d\u9700\u8981\u5355\u72ec\u5b89\u88c5os\u5e93\u3001os\u5e93\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\u3001\u53ef\u4ee5\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u3002<\/strong>Python\u7684os\u5e93\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u529f\u80fd\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fdb\u884c\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u7ba1\u7406\u3001\u73af\u5883\u53d8\u91cf\u7684\u83b7\u53d6\u548c\u8bbe\u7f6e\u7b49\u64cd\u4f5c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdos\u5e93\u7684\u529f\u80fd\u53ca\u5176\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u4e00\u4e9b\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001OS\u5e93\u7684\u57fa\u672c\u529f\u80fd<\/h3>\n<\/p>\n<p><p>Python\u7684os\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u51fd\u6570\u6765\u5904\u7406\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u7684\u4efb\u52a1\u3002\u65e0\u8bba\u662f\u6587\u4ef6\u64cd\u4f5c\u3001\u76ee\u5f55\u7ba1\u7406\u8fd8\u662f\u8fdb\u7a0b\u63a7\u5236\uff0cos\u5e93\u90fd\u80fd\u63d0\u4f9b\u4fbf\u6377\u7684\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><h4>1. \u6587\u4ef6\u548c\u76ee\u5f55\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>os\u5e93\u5141\u8bb8\u4f60\u8bbf\u95ee\u64cd\u4f5c\u7cfb\u7edf\u7684\u5e95\u5c42\u6587\u4ef6\u7cfb\u7edf\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7os.listdir()\u5217\u51fa\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u3002os.mkdir()\u548cos.rmdir()\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u548c\u5220\u9664\u76ee\u5f55\u3002\u800cos.remove()\u5219\u7528\u4e8e\u5220\u9664\u6587\u4ef6\u3002os\u5e93\u8fd8\u63d0\u4f9b\u4e86os.path\u6a21\u5757\uff0c\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u8def\u5f84\uff0c\u4f8b\u5982os.path.join()\u7528\u4e8e\u8def\u5f84\u62fc\u63a5\uff0cos.path.exists()\u7528\u4e8e\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><h4>2. \u73af\u5883\u53d8\u91cf\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>os\u5e93\u53ef\u4ee5\u7528\u4e8e\u8bbf\u95ee\u548c\u4fee\u6539\u73af\u5883\u53d8\u91cf\u3002\u901a\u8fc7os.environ\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u83b7\u53d6\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u7684\u503c\uff0c\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u65b0\u7684\u73af\u5883\u53d8\u91cf\u3002os.getenv()\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u5b89\u5168\u5730\u83b7\u53d6\u73af\u5883\u53d8\u91cf\uff0c\u800cos.putenv()\u51fd\u6570\u5219\u53ef\u4ee5\u7528\u6765\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001OS\u5e93\u5728\u6587\u4ef6\u548c\u76ee\u5f55\u7ba1\u7406\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>os\u5e93\u5728\u6587\u4ef6\u548c\u76ee\u5f55\u7ba1\u7406\u4e2d\u7684\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u8de8\u5e73\u53f0\u5f00\u53d1\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<\/p>\n<p><h4>1. \u5217\u51fa\u76ee\u5f55\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528os.listdir()\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\u7684\u540d\u79f0\u5217\u8868\u3002\u8fd9\u5728\u9700\u8981\u904d\u5386\u76ee\u5f55\u7ed3\u6784\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u9700\u8981\u6279\u91cf\u5904\u7406\u67d0\u4e2a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7os.listdir()\u83b7\u53d6\u6587\u4ef6\u5217\u8868\uff0c\u7136\u540e\u9010\u4e00\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def list_files_in_directory(directory_path):<\/p>\n<p>    try:<\/p>\n<p>        files = os.listdir(directory_path)<\/p>\n<p>        print(f&quot;Files in {directory_path}:&quot;)<\/p>\n<p>        for file in files:<\/p>\n<p>            print(file)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>list_files_in_directory(&quot;.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u548c\u5220\u9664\u76ee\u5f55<\/h4>\n<\/p>\n<p><p>\u5728\u81ea\u52a8\u5316\u811a\u672c\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u52a8\u6001\u521b\u5efa\u548c\u5220\u9664\u76ee\u5f55\u3002os\u5e93\u63d0\u4f9b\u4e86os.mkdir()\u548cos.rmdir()\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e9b\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5728\u5907\u4efd\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2a\u5907\u4efd\u76ee\u5f55\uff0c\u7136\u540e\u5c06\u6587\u4ef6\u590d\u5236\u8fdb\u53bb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_and_remove_directory(directory_name):<\/p>\n<p>    try:<\/p>\n<p>        os.mkdir(directory_name)<\/p>\n<p>        print(f&quot;Directory {directory_name} created.&quot;)<\/p>\n<p>    except FileExistsError:<\/p>\n<p>        print(f&quot;Directory {directory_name} already exists.&quot;)<\/p>\n<p>    try:<\/p>\n<p>        os.rmdir(directory_name)<\/p>\n<p>        print(f&quot;Directory {directory_name} removed.&quot;)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;Directory {directory_name} does not exist.&quot;)<\/p>\n<p>    except OSError:<\/p>\n<p>        print(f&quot;Directory {directory_name} is not empty.&quot;)<\/p>\n<p>create_and_remove_directory(&quot;test_directory&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001OS\u5e93\u5728\u73af\u5883\u53d8\u91cf\u64cd\u4f5c\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u914d\u7f6e\u4e0d\u540c\u73af\u5883\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u64cd\u4f5c\u73af\u5883\u53d8\u91cf\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\uff0c\u7a0b\u5e8f\u53ef\u80fd\u9700\u8981\u6839\u636e\u73af\u5883\u53d8\u91cf\u8c03\u6574\u81ea\u8eab\u7684\u884c\u4e3a\u3002os.getenv()\u51fd\u6570\u53ef\u4ee5\u5b89\u5168\u5730\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u7684\u503c\uff0c\u5982\u679c\u73af\u5883\u53d8\u91cf\u4e0d\u5b58\u5728\uff0c\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_environment_variable(variable_name, default_value=None):<\/p>\n<p>    value = os.getenv(variable_name, default_value)<\/p>\n<p>    print(f&quot;Environment variable &#39;{variable_name}&#39;: {value}&quot;)<\/p>\n<p>    return value<\/p>\n<p>get_environment_variable(&quot;HOME&quot;, &quot;\/home\/user&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5728\u8fd0\u884c\u65f6\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u4ee5\u5f71\u54cd\u540e\u7eed\u7a0b\u5e8f\u7684\u884c\u4e3a\u3002os\u5e93\u63d0\u4f9b\u4e86os.putenv()\u51fd\u6570\u7528\u4e8e\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def set_environment_variable(variable_name, value):<\/p>\n<p>    os.putenv(variable_name, value)<\/p>\n<p>    print(f&quot;Environment variable &#39;{variable_name}&#39; set to {value}&quot;)<\/p>\n<p>set_environment_variable(&quot;MY_ENV_VAR&quot;, &quot;test_value&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001OS\u5e93\u5728\u8fdb\u7a0b\u7ba1\u7406\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>os\u5e93\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u7528\u4e8e\u8fdb\u7a0b\u7ba1\u7406\uff0c\u5982fork\u3001exec\u3001w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u5b50\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p>os\u5e93\u7684os.fork()\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b50\u8fdb\u7a0b\u3002\u5728\u5b50\u8fdb\u7a0b\u4e2d\uff0cos.getpid()\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u8fdb\u7a0b\u7684ID\u3002\u8fd9\u5728\u9700\u8981\u5e76\u884c\u5904\u7406\u4efb\u52a1\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def create_subprocess():<\/p>\n<p>    pid = os.fork()<\/p>\n<p>    if pid == 0:<\/p>\n<p>        print(f&quot;Child process ID: {os.getpid()}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Parent process ID: {os.getpid()}, Child process ID: {pid}&quot;)<\/p>\n<p>create_subprocess()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6267\u884c\u5916\u90e8\u7a0b\u5e8f<\/h4>\n<\/p>\n<p><p>os\u5e93\u7684os.exec()\u7cfb\u5217\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u5916\u90e8\u7a0b\u5e8f\u3002\u8fd9\u5728\u9700\u8981\u8c03\u7528\u5916\u90e8\u547d\u4ee4\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528os.execlp()\u6267\u884c\u4e00\u4e2ashell\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def execute_external_program(command):<\/p>\n<p>    try:<\/p>\n<p>        os.execlp(command, command)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;Command &#39;{command}&#39; not found.&quot;)<\/p>\n<h2><strong>execute_external_program(&quot;ls&quot;)  # Uncomment to test<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001OS\u5e93\u5728\u8de8\u5e73\u53f0\u5f00\u53d1\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>os\u5e93\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u70b9\u662f\u5b83\u4e3a\u8de8\u5e73\u53f0\u5f00\u53d1\u63d0\u4f9b\u4e86\u7edf\u4e00\u7684\u63a5\u53e3\u3002\u901a\u8fc7\u4f7f\u7528os\u5e93\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u7f16\u5199\u9002\u7528\u4e8e\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>1. \u8def\u5f84\u64cd\u4f5c\u7684\u8de8\u5e73\u53f0\u652f\u6301<\/h4>\n<\/p>\n<p><p>\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8def\u5f84\u5206\u9694\u7b26\u4e0d\u540c\uff0cos\u5e93\u901a\u8fc7os.path\u6a21\u5757\u4e3a\u8def\u5f84\u64cd\u4f5c\u63d0\u4f9b\u4e86\u8de8\u5e73\u53f0\u652f\u6301\u3002\u4f8b\u5982\uff0cos.path.join()\u4f1a\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u9009\u62e9\u5408\u9002\u7684\u8def\u5f84\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def join_paths(*paths):<\/p>\n<p>    full_path = os.path.join(*paths)<\/p>\n<p>    print(f&quot;Joined path: {full_path}&quot;)<\/p>\n<p>    return full_path<\/p>\n<p>join_paths(&quot;folder&quot;, &quot;subfolder&quot;, &quot;file.txt&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5e73\u53f0\u7279\u5b9a\u7684\u529f\u80fd<\/h4>\n<\/p>\n<p><p>os\u5e93\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5e73\u53f0\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u4f8b\u5982os.name\u53ef\u4ee5\u8fd4\u56de\u5f53\u524d\u64cd\u4f5c\u7cfb\u7edf\u7684\u540d\u79f0\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u9009\u62e9\u4e0d\u540c\u7684\u5b9e\u73b0\uff0c\u4ee5\u5b9e\u73b0\u66f4\u597d\u7684\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def check_operating_system():<\/p>\n<p>    os_name = os.name<\/p>\n<p>    if os_name == &#39;posix&#39;:<\/p>\n<p>        print(&quot;Operating system: Unix\/Linux\/MacOS&quot;)<\/p>\n<p>    elif os_name == &#39;nt&#39;:<\/p>\n<p>        print(&quot;Operating system: Windows&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Operating system: {os_name}&quot;)<\/p>\n<p>check_operating_system()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001OS\u5e93\u5728\u5b89\u5168\u6027\u548c\u6743\u9650\u7ba1\u7406\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u548c\u8fdb\u7a0b\u65f6\uff0c\u5b89\u5168\u6027\u548c\u6743\u9650\u7ba1\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002os\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u6765\u5e2e\u52a9\u5904\u7406\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1. \u4fee\u6539\u6587\u4ef6\u6743\u9650<\/h4>\n<\/p>\n<p><p>os\u5e93\u7684os.chmod()\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u4fee\u6539\u6587\u4ef6\u7684\u6743\u9650\u3002\u8fd9\u5728\u9700\u8981\u63a7\u5236\u6587\u4ef6\u7684\u8bfb\u5199\u6743\u9650\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def change_file_permission(file_path, mode):<\/p>\n<p>    try:<\/p>\n<p>        os.chmod(file_path, mode)<\/p>\n<p>        print(f&quot;Permissions of &#39;{file_path}&#39; changed to {oct(mode)}&quot;)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;File &#39;{file_path}&#39; not found.&quot;)<\/p>\n<p>    except PermissionError:<\/p>\n<p>        print(f&quot;Permission denied when changing permissions of &#39;{file_path}&#39;.&quot;)<\/p>\n<h2><strong>change_file_permission(&quot;example.txt&quot;, 0o644)  # Uncomment to test<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7528\u6237\u548c\u7ec4\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>os\u5e93\u8fd8\u63d0\u4f9b\u4e86\u83b7\u53d6\u7528\u6237\u548c\u7ec4\u4fe1\u606f\u7684\u529f\u80fd\uff0c\u4f8b\u5982os.getuid()\u548cos.getgid()\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684\u7528\u6237ID\u548c\u7ec4ID\u3002\u8fd9\u5728\u9700\u8981\u6839\u636e\u7528\u6237\u8eab\u4efd\u8fdb\u884c\u6743\u9650\u63a7\u5236\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_user_and_group_info():<\/p>\n<p>    user_id = os.getuid()<\/p>\n<p>    group_id = os.getgid()<\/p>\n<p>    print(f&quot;User ID: {user_id}, Group ID: {group_id}&quot;)<\/p>\n<p>get_user_and_group_info()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001OS\u5e93\u5728\u65f6\u95f4\u548c\u65e5\u671f\u64cd\u4f5c\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u867d\u7136os\u5e93\u4e0d\u662f\u4e13\u95e8\u7528\u4e8e\u65f6\u95f4\u548c\u65e5\u671f\u64cd\u4f5c\u7684\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u76f8\u5173\u529f\u80fd\uff0c\u5982\u83b7\u53d6\u6587\u4ef6\u7684\u4fee\u6539\u65f6\u95f4\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u6587\u4ef6\u7684\u4fee\u6539\u65f6\u95f4<\/h4>\n<\/p>\n<p><p>os\u5e93\u7684os.path.getmtime()\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u6587\u4ef6\u7684\u6700\u540e\u4fee\u6539\u65f6\u95f4\u3002\u8fd9\u5728\u9700\u8981\u6839\u636e\u6587\u4ef6\u66f4\u65b0\u65f6\u95f4\u8fdb\u884c\u64cd\u4f5c\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def get_file_modification_time(file_path):<\/p>\n<p>    try:<\/p>\n<p>        modification_time = os.path.getmtime(file_path)<\/p>\n<p>        formatted_time = time.ctime(modification_time)<\/p>\n<p>        print(f&quot;Last modification time of &#39;{file_path}&#39;: {formatted_time}&quot;)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;File &#39;{file_path}&#39; not found.&quot;)<\/p>\n<h2><strong>get_file_modification_time(&quot;example.txt&quot;)  # Uncomment to test<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/h4>\n<\/p>\n<p><p>\u867d\u7136\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u901a\u5e38\u4f7f\u7528time\u6a21\u5757\uff0c\u4f46os\u5e93\u7684os.times()\u51fd\u6570\u53ef\u4ee5\u63d0\u4f9b\u8fdb\u7a0b\u7684CPU\u65f6\u95f4\u7b49\u4fe1\u606f\uff0c\u8fd9\u5728\u6027\u80fd\u5206\u6790\u65f6\u53ef\u80fd\u4f1a\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_process_times():<\/p>\n<p>    process_times = os.times()<\/p>\n<p>    print(f&quot;User time: {process_times.user}, System time: {process_times.system}&quot;)<\/p>\n<p>get_process_times()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001OS\u5e93\u5728\u9519\u8bef\u5904\u7406\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528os\u5e93\u8fdb\u884c\u64cd\u4f5c\u7cfb\u7edf\u7ea7\u522b\u7684\u64cd\u4f5c\u65f6\uff0c\u9519\u8bef\u5904\u7406\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002os\u5e93\u4e2d\u7684\u5927\u591a\u6570\u51fd\u6570\u90fd\u4f1a\u5728\u51fa\u9519\u65f6\u629b\u51fa\u5f02\u5e38\uff0c\u5f00\u53d1\u8005\u9700\u8981\u901a\u8fc7\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u6765\u5904\u7406\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h4>1. \u6355\u83b7\u6587\u4ef6\u64cd\u4f5c\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u3002\u901a\u8fc7\u6355\u83b7\u8fd9\u4e9b\u9519\u8bef\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_file(file_path):<\/p>\n<p>    try:<\/p>\n<p>        with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>            content = file.read()<\/p>\n<p>            print(content)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;File &#39;{file_path}&#39; not found.&quot;)<\/p>\n<p>    except PermissionError:<\/p>\n<p>        print(f&quot;Permission denied when reading &#39;{file_path}&#39;.&quot;)<\/p>\n<p>read_file(&quot;example.txt&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6355\u83b7\u8fdb\u7a0b\u64cd\u4f5c\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u548c\u7ba1\u7406\u8fdb\u7a0b\u65f6\uff0c\u4e5f\u53ef\u80fd\u4f1a\u9047\u5230\u9519\u8bef\uff0c\u5982\u8d44\u6e90\u4e0d\u8db3\u7b49\u3002\u901a\u8fc7\u6355\u83b7\u8fd9\u4e9b\u9519\u8bef\uff0c\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u5f02\u5e38\u60c5\u51b5\u4e0b\u4ecd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_process_with_error_handling():<\/p>\n<p>    try:<\/p>\n<p>        pid = os.fork()<\/p>\n<p>        if pid == 0:<\/p>\n<p>            print(f&quot;Child process ID: {os.getpid()}&quot;)<\/p>\n<p>        else:<\/p>\n<p>            print(f&quot;Parent process ID: {os.getpid()}, Child process ID: {pid}&quot;)<\/p>\n<p>    except OSError as e:<\/p>\n<p>        print(f&quot;An error occurred during fork: {e}&quot;)<\/p>\n<p>create_process_with_error_handling()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001OS\u5e93\u5728\u811a\u672c\u548c\u81ea\u52a8\u5316\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>os\u5e93\u5728\u811a\u672c\u548c\u81ea\u52a8\u5316\u4efb\u52a1\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u573a\u666f\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>1. \u6279\u91cf\u6587\u4ef6\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5728\u6279\u91cf\u5904\u7406\u6587\u4ef6\u65f6\uff0cos\u5e93\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u63a5\u53e3\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528os.walk()\u904d\u5386\u76ee\u5f55\u6811\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def batch_process_files(directory_path):<\/p>\n<p>    for root, dirs, files in os.walk(directory_path):<\/p>\n<p>        for file in files:<\/p>\n<p>            file_path = os.path.join(root, file)<\/p>\n<p>            print(f&quot;Processing file: {file_path}&quot;)<\/p>\n<p>            # Add file processing logic here<\/p>\n<p>batch_process_files(&quot;.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8c03\u7528\u5916\u90e8\u547d\u4ee4<\/h4>\n<\/p>\n<p><p>\u5728\u81ea\u52a8\u5316\u811a\u672c\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u8c03\u7528\u5916\u90e8\u547d\u4ee4\u3002os.system()\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u4e00\u4e2ashell\u547d\u4ee4\uff0c\u5e76\u8fd4\u56de\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def run_shell_command(command):<\/p>\n<p>    exit_status = os.system(command)<\/p>\n<p>    print(f&quot;Command &#39;{command}&#39; exited with status {exit_status}&quot;)<\/p>\n<h2><strong>run_shell_command(&quot;ls&quot;)  # Uncomment to test<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9os\u5e93\u7684\u5168\u9762\u4e86\u89e3\u548c\u7075\u6d3b\u5e94\u7528\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u5f3a\u5927\u7684\u64cd\u4f5c\u7cfb\u7edf\u7ea7\u522b\u7684\u529f\u80fd\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u548c\u53ef\u79fb\u690d\u6027\u3002\u65e0\u8bba\u662f\u6587\u4ef6\u64cd\u4f5c\u3001\u8fdb\u7a0b\u7ba1\u7406\u8fd8\u662f\u73af\u5883\u53d8\u91cf\u5904\u7406\uff0cos\u5e93\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a5\u53e3\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u8f7b\u677e\u5b9e\u73b0\u5404\u79cd\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165os\u5e93\uff1f<\/strong><br \/>os\u5e93\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u65e0\u9700\u5355\u72ec\u5b89\u88c5\u3002\u53ea\u9700\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528<code>import os<\/code>\u547d\u4ee4\u5373\u53ef\u5bfc\u5165\u8be5\u5e93\u5e76\u5f00\u59cb\u4f7f\u7528\u5176\u63d0\u4f9b\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>\u4f7f\u7528os\u5e93\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u529f\u80fd\uff1f<\/strong><br \/>os\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u7684\u529f\u80fd\uff0c\u5982\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u521b\u5efa\u3001\u5220\u9664\u3001\u91cd\u547d\u540d\uff0c\u83b7\u53d6\u73af\u5883\u53d8\u91cf\uff0c\u8fd0\u884c\u7cfb\u7edf\u547d\u4ee4\u7b49\u3002\u5e38\u7528\u7684\u51fd\u6570\u5305\u62ec<code>os.listdir()<\/code>\u7528\u4e8e\u5217\u51fa\u76ee\u5f55\u5185\u5bb9\uff0c<code>os.mkdir()<\/code>\u7528\u4e8e\u521b\u5efa\u65b0\u76ee\u5f55\uff0c\u4ee5\u53ca<code>os.remove()<\/code>\u7528\u4e8e\u5220\u9664\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u5728\u4f7f\u7528os\u5e93\u65f6\u9047\u5230\u9519\u8bef\uff0c\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u5982\u679c\u5728\u4f7f\u7528os\u5e93\u65f6\u51fa\u73b0\u9519\u8bef\uff0c\u9996\u5148\u53ef\u4ee5\u68c0\u67e5\u4f60\u7684Python\u73af\u5883\u662f\u5426\u6b63\u5e38\u3002\u786e\u4fdd\u4f60\u6ca1\u6709\u62fc\u5199\u9519\u8bef\uff0c\u5e76\u4e14\u5728\u8c03\u7528\u51fd\u6570\u65f6\u4f7f\u7528\u6b63\u786e\u7684\u53c2\u6570\u3002\u5982\u679c\u95ee\u9898\u4f9d\u65e7\u5b58\u5728\uff0c\u5efa\u8bae\u67e5\u770b\u9519\u8bef\u4fe1\u606f\u5e76\u5728\u7f51\u4e0a\u641c\u7d22\u76f8\u5173\u89e3\u51b3\u65b9\u6848\uff0c\u6216\u8005\u8bbf\u95eePython\u7684\u5b98\u65b9\u6587\u6863\u83b7\u53d6\u66f4\u591a\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e0d\u9700\u8981\u5355\u72ec\u5b89\u88c5os\u5e93\u3001os\u5e93\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\u3001\u53ef\u4ee5\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u3002Python [&hellip;]","protected":false},"author":3,"featured_media":1019694,"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\/1019678"}],"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=1019678"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019678\/revisions"}],"predecessor-version":[{"id":1019695,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019678\/revisions\/1019695"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1019694"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1019678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1019678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1019678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}