{"id":599,"date":"2019-10-23T14:19:40","date_gmt":"2019-10-23T06:19:40","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=599"},"modified":"2023-04-18T15:57:32","modified_gmt":"2023-04-18T07:57:32","slug":"general-collection-of-python-common-modules-sorting","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2019\/10\/general-collection-of-python-common-modules-sorting\/","title":{"rendered":"Python \u5e38\u7528\u6a21\u5757\u5927\u5168\uff08\u6574\u7406\uff09"},"content":{"rendered":"\n

OS \u6a21\u5757<\/strong><\/h2>\n\n\n\n
\n<\/em>#os\u6a21\u5757\u5c31\u662f\u5bf9\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f7f\u7528\u8be5\u6a21\u5757\u5fc5\u987b\u5148\u5bfc\u5165\u6a21\u5757\uff1a\nimport os\n\n#getcwd() \u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55(\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u9ed8\u8ba4\u90fd\u662f\u5f53\u524d\u6587\u4ef6\u6240\u5728\u7684\u6587\u4ef6\u5939)\nresult = os.getcwd()\nprint(result)\n\n#chdir()\u6539\u53d8\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\nos.chdir('\/home\/sy')\nresult = os.getcwd()\nprint(result)\n\nopen('02.txt','w')\n\n#\u64cd\u4f5c\u65f6\u5982\u679c\u4e66\u5199\u5b8c\u6574\u7684\u8def\u5f84\u5219\u4e0d\u9700\u8981\u8003\u8651\u9ed8\u8ba4\u5de5\u4f5c\u76ee\u5f55\u7684\u95ee\u9898,\u6309\u7167\u5b9e\u9645\u4e66\u5199\u8def\u5f84\u64cd\u4f5c\nopen('\/home\/sy\/\u4e0b\u8f7d\/02.txt','w')\n\n#listdir() \u83b7\u53d6\u6307\u5b9a\u6587\u4ef6\u5939\u4e2d\u6240\u6709\u5185\u5bb9\u7684\u540d\u79f0\u5217\u8868\nresult = os.listdir('\/home\/sy')\nprint(result)\n\n#mkdir()  \u521b\u5efa\u6587\u4ef6\u5939\n#os.mkdir('girls')\n#os.mkdir('boys',0o777)\n\n#makedirs()  \u9012\u5f52\u521b\u5efa\u6587\u4ef6\u5939\n#os.makedirs('\/home\/sy\/a\/b\/c\/d')\n\n#rmdir() \u5220\u9664\u7a7a\u76ee\u5f55\n#os.rmdir('girls')\n\n#removedirs \u9012\u5f52\u5220\u9664\u6587\u4ef6\u5939  \u5fc5\u987b\u90fd\u662f\u7a7a\u76ee\u5f55\n#os.removedirs('\/home\/sy\/a\/b\/c\/d')\n\n#rename() \u6587\u4ef6\u6216\u6587\u4ef6\u5939\u91cd\u547d\u540d\n#os.rename('\/home\/sy\/a','\/home\/sy\/alibaba'\n#os.rename('02.txt','002.txt')\n\n#stat() \u83b7\u53d6\u6587\u4ef6\u6216\u8005\u6587\u4ef6\u5939\u7684\u4fe1\u606f\n#result = os.stat('\/home\/sy\/PycharmProject\/Python3\/10.27\/01.py)\n#print(result)\n\n#system() \u6267\u884c\u7cfb\u7edf\u547d\u4ee4(\u5371\u9669\u51fd\u6570)\n#result = os.system('ls -al')  #\u83b7\u53d6\u9690\u85cf\u6587\u4ef6\n#print(result)\n\n#\u73af\u5883\u53d8\u91cf\n'''\n\u73af\u5883\u53d8\u91cf\u5c31\u662f\u4e00\u4e9b\u547d\u4ee4\u7684\u96c6\u5408\n\u64cd\u4f5c\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u5c31\u662f\u64cd\u4f5c\u7cfb\u7edf\u5728\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u65f6\u641c\u7d22\u547d\u4ee4\u7684\u76ee\u5f55\u7684\u96c6\u5408\n'''\n#getenv() \u83b7\u53d6\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\nresult = os.getenv('PATH')\nprint(result.split(':'))\n\n#putenv() \u5c06\u4e00\u4e2a\u76ee\u5f55\u6dfb\u52a0\u5230\u73af\u5883\u53d8\u91cf\u4e2d(\u4e34\u65f6\u589e\u52a0\u4ec5\u5bf9\u5f53\u524d\u811a\u672c\u6709\u6548)\n#os.putenv('PATH','\/home\/sy\/\u4e0b\u8f7d')\n#os.system('syls')\n\n#exit() \u9000\u51fa\u7ec8\u7aef\u7684\u547d\u4ee4\n\n#os\u6a21\u5757\u4e2d\u7684\u5e38\u7528\u503c\n#curdir  \u8868\u793a\u5f53\u524d\u6587\u4ef6\u5939   .\u8868\u793a\u5f53\u524d\u6587\u4ef6\u5939  \u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u4ee5\u7701\u7565\nprint(os.curdir)\n\n#pardir  \u8868\u793a\u4e0a\u4e00\u5c42\u6587\u4ef6\u5939   ..\u8868\u793a\u4e0a\u4e00\u5c42\u6587\u4ef6\u5939  \u4e0d\u53ef\u7701\u7565!\nprint(os.pardir)\n\n#os.mkdir('..\/..\/..\/man')#\u76f8\u5bf9\u8def\u5f84  \u4ece\u5f53\u524d\u76ee\u5f55\u5f00\u59cb\u67e5\u627e\n#os.mkdir('\/home\/sy\/man1')#\u7edd\u5bf9\u8def\u5f84  \u4ece\u6839\u76ee\u5f55\u5f00\u59cb\u67e5\u627e\n\n#name \u83b7\u53d6\u4ee3\u8868\u64cd\u4f5c\u7cfb\u7edf\u7684\u540d\u79f0\u5b57\u7b26\u4e32\nprint(os.name) #posix -> linux\u6216\u8005unix\u7cfb\u7edf  nt -> window\u7cfb\u7edf\n\n#sep \u83b7\u53d6\u7cfb\u7edf\u8def\u5f84\u95f4\u9694\u7b26\u53f7  window ->\\    linux ->\/\nprint(os.sep)\n\n#extsep \u83b7\u53d6\u6587\u4ef6\u540d\u79f0\u548c\u540e\u7f00\u4e4b\u95f4\u7684\u95f4\u9694\u7b26\u53f7  window & linux -> .\nprint(os.extsep)\n\n#linesep  \u83b7\u53d6\u64cd\u4f5c\u7cfb\u7edf\u7684\u6362\u884c\u7b26\u53f7  window -> \\r\\n  linux\/unix -> \\n\nprint(repr(os.linesep))\n\n#\u4ee5\u4e0b\u5185\u5bb9\u90fd\u662fos.path\u5b50\u6a21\u5757\u4e2d\u7684\u5185\u5bb9\n\n#abspath()  \u5c06\u76f8\u5bf9\u8def\u5f84\u8f6c\u5316\u4e3a\u7edd\u5bf9\u8def\u5f84\npath = '.\/boys'#\u76f8\u5bf9\nresult = os.path.abspath(path)\nprint(result)\n\n#dirname()  \u83b7\u53d6\u5b8c\u6574\u8def\u5f84\u5f53\u4e2d\u7684\u76ee\u5f55\u90e8\u5206  &  basename()\u83b7\u53d6\u5b8c\u6574\u8def\u5f84\u5f53\u4e2d\u7684\u4e3b\u4f53\u90e8\u5206\npath = '\/home\/sy\/boys'\nresult = os.path.dirname(path)\nprint(result)\n\nresult = os.path.basename(path)\nprint(result)\n\n#split() \u5c06\u4e00\u4e2a\u5b8c\u6574\u7684\u8def\u5f84\u5207\u5272\u6210\u76ee\u5f55\u90e8\u5206\u548c\u4e3b\u4f53\u90e8\u5206\npath = '\/home\/sy\/boys'\nresult = os.path.split(path)\nprint(result)\n\n#join() \u5c062\u4e2a\u8def\u5f84\u5408\u5e76\u6210\u4e00\u4e2a\nvar1 = '\/home\/sy'\nvar2 = '000.py'\nresult = os.path.join(var1,var2)\nprint(result)\n\n#splitext() \u5c06\u4e00\u4e2a\u8def\u5f84\u5207\u5272\u6210\u6587\u4ef6\u540e\u7f00\u548c\u5176\u4ed6\u4e24\u4e2a\u90e8\u5206,\u4e3b\u8981\u7528\u4e8e\u83b7\u53d6\u6587\u4ef6\u7684\u540e\u7f00\npath = '\/home\/sy\/000.py'\nresult = os.path.splitext(path)\nprint(result)\n\n#getsize()  \u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\n#path = '\/home\/sy\/000.py'\n#result = os.path.getsize(path)\n#print(result)\n\n#isfile() \u68c0\u6d4b\u662f\u5426\u662f\u6587\u4ef6\npath = '\/home\/sy\/000.py'\nresult = os.path.isfile(path)\nprint(result)\n\n#isdir()  \u68c0\u6d4b\u662f\u5426\u662f\u6587\u4ef6\u5939\nresult = os.path.isdir(path)\nprint(result)\n\n#islink() \u68c0\u6d4b\u662f\u5426\u662f\u94fe\u63a5\npath = '\/initrd.img.old'\nresult = os.path.islink(path)\nprint(result)\n\n#getctime() \u83b7\u53d6\u6587\u4ef6\u7684\u521b\u5efa\u65f6\u95f4 get create time\n#getmtime() \u83b7\u53d6\u6587\u4ef6\u7684\u4fee\u6539\u65f6\u95f4 get modify time\n#getatime() \u83b7\u53d6\u6587\u4ef6\u7684\u8bbf\u95ee\u65f6\u95f4 get active time\n\nimport time\n\nfilepath = '\/home\/sy\/\u4e0b\u8f7d\/chls'\n\nresult = os.path.getctime(filepath)\nprint(time.ctime(result))\n\nresult = os.path.getmtime(filepath)\nprint(time.ctime(result))\n\nresult = os.path.getatime(filepath)\nprint(time.ctime(result))\n\n#exists() \u68c0\u6d4b\u67d0\u4e2a\u8def\u5f84\u662f\u5426\u771f\u5b9e\u5b58\u5728\nfilepath = '\/home\/sy\/\u4e0b\u8f7d\/chls'\nresult = os.path.exists(filepath)\nprint(result)\n\n#isabs() \u68c0\u6d4b\u4e00\u4e2a\u8def\u5f84\u662f\u5426\u662f\u7edd\u5bf9\u8def\u5f84\npath = '\/boys'\nresult = os.path.isabs(path)\nprint(result)\n\n#samefile() \u68c0\u6d4b2\u4e2a\u8def\u5f84\u662f\u5426\u662f\u540c\u4e00\u4e2a\u6587\u4ef6\npath1 = '\/home\/sy\/\u4e0b\u8f7d\/001'\npath2 = '..\/..\/..\/\u4e0b\u8f7d\/001'\nresult = os.path.samefile(path1,path2)\nprint(result)\n\n#os.environ \u7528\u4e8e\u83b7\u53d6\u548c\u8bbe\u7f6e\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u7684\u5185\u7f6e\u503c\nimport os\n#\u83b7\u53d6\u7cfb\u7edf\u73af\u5883\u53d8\u91cf  getenv() \u6548\u679c\nprint(os.environ['PATH'])\n\n#\u8bbe\u7f6e\u7cfb\u7edf\u73af\u5883\u53d8\u91cf putenv()\nos.environ['PATH'] += ':\/home\/sy\/\u4e0b\u8f7d'\nos.system('chls')<\/pre>\n\n\n\n

os \u5e38\u7528\u65b9\u6cd5<\/h4>\n\n\n\n