{"id":4188,"date":"2022-03-19T22:06:10","date_gmt":"2022-03-19T14:06:10","guid":{"rendered":"https:\/\/egonlin.com\/?p=4188"},"modified":"2022-04-26T13:45:19","modified_gmt":"2022-04-26T05:45:19","slug":"%e7%ac%ac%e5%9b%9b%e7%af%87%ef%bc%9a%e5%b8%b8%e7%94%a8%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=4188","title":{"rendered":"\u7b2c\u56db\u7bc7\uff1a\u5e38\u7528\u6a21\u5757"},"content":{"rendered":"<h1>Python\u5e38\u7528\u6a21\u5757<\/h1>\n<h2>time\u6a21\u5757<\/h2>\n<pre><code class=\"language-python\">import time<\/code><\/pre>\n<h3>\u65f6\u95f4\u6233<\/h3>\n<p>&emsp;&emsp;\u65f6\u95f4\u6233(timestamp)\uff1a\u65f6\u95f4\u6233\u8868\u793a\u7684\u662f\u4ece1970\u5e741\u67081\u65e500:00:00\u5f00\u59cb\u6309\u79d2\u8ba1\u7b97\u7684\u504f\u79fb\u91cf\u3002<\/p>\n<pre><code class=\"language-python\">time_stamp = time.time()\nprint(time_stamp, type(time_stamp))<\/code><\/pre>\n<pre><code>1552551519.291029 <class 'float'><\/code><\/pre>\n<h3>\u683c\u5f0f\u5316\u65f6\u95f4<\/h3>\n<p>&emsp;&emsp;\u683c\u5f0f\u5316\u7684\u65f6\u95f4\u5b57\u7b26\u4e32(format string)\uff1a\u683c\u5f0f\u5316\u65f6\u95f4\u8868\u793a\u7684\u662f\u666e\u901a\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u65f6\u95f4\u3002<\/p>\n<pre><code class=\"language-python\">format_time = time.strftime(&quot;%Y-%m-%d %X&quot;)\nprint(format_time, type(format_time))<\/code><\/pre>\n<pre><code>2019-03-07 16:22:11 <class 'str'><\/code><\/pre>\n<h3>\u7ed3\u6784\u5316\u65f6\u95f4<\/h3>\n<p>&emsp;&emsp;\u7ed3\u6784\u5316\u7684\u65f6\u95f4(struct time)\uff1astruct_time\u5143\u7ec4\u5171\u67099\u4e2a\u5143\u7d20\u5171\u4e5d\u4e2a\u5143\u7d20\uff0c\u5206\u522b\u4e3a(\u5e74\uff0c\u6708\uff0c\u65e5\uff0c\u65f6\uff0c\u5206\uff0c\u79d2\uff0c\u4e00\u5e74\u4e2d\u7b2c\u51e0\u5468\uff0c\u4e00\u5e74\u4e2d\u7b2c\u51e0\u5929\uff0c\u590f\u4ee4\u65f6)<\/p>\n<pre><code class=\"language-python\">print(&#039;\u672c\u5730\u65f6\u533a\u7684struct_time:\\n{}&#039;.format(time.localtime()))\nprint(&#039;UTC\u65f6\u533a\u7684struct_time:\\n{}&#039;.format(time.gmtime()))<\/code><\/pre>\n<pre><code>\u672c\u5730\u65f6\u533a\u7684struct_time:\ntime.struct_time(tm_year=2019, tm_mon=3, tm_mday=7, tm_hour=16, tm_min=22, tm_sec=11, tm_wday=3, tm_yday=66, tm_isdst=0)\nUTC\u65f6\u533a\u7684struct_time:\ntime.struct_time(tm_year=2019, tm_mon=3, tm_mday=7, tm_hour=8, tm_min=22, tm_sec=11, tm_wday=3, tm_yday=66, tm_isdst=0)<\/code><\/pre>\n<pre><code class=\"language-python\"># \u7ed3\u6784\u5316\u65f6\u95f4\u7684\u57fa\u51c6\u65f6\u95f4\nprint(time.localtime(0))<\/code><\/pre>\n<pre><code>time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)<\/code><\/pre>\n<pre><code class=\"language-python\"># \u7ed3\u6784\u5316\u65f6\u95f4\u7684\u57fa\u51c6\u65f6\u95f4\u4e0a\u589e\u52a0\u4e00\u5e74\u65f6\u95f4\nprint(time.localtime(3600*24*365))<\/code><\/pre>\n<pre><code>time.struct_time(tm_year=1971, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=1, tm_isdst=0)<\/code><\/pre>\n<h3>\u4e0d\u540c\u683c\u5f0f\u65f6\u95f4\u7684\u8f6c\u6362<\/h3>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/03\/\u7ed3\u6784\u5316\u65f6\u95f4-\u683c\u5f0f\u5316\u65f6\u95f4-\u65f6\u95f4\u6233.jpg'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/03\/\u7ed3\u6784\u5316\u65f6\u95f4-\u683c\u5f0f\u5316\u65f6\u95f4-\u65f6\u95f4\u6233.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>&emsp;&emsp;\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u6211\u4eec\u603b\u80fd\u901a\u8fc7\u67d0\u4e9b\u65b9\u6cd5\u5728\u7ed3\u6784\u5316\u65f6\u95f4-\u683c\u5f0f\u5316\u65f6\u95f4-\u65f6\u95f4\u6233\u4e09\u8005\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff0c\u4e0b\u9762\u6211\u4eec\u5c06\u7528\u4ee3\u7801\u5c55\u793a\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u8f6c\u6362\u65f6\u95f4\u683c\u5f0f\u3002<\/p>\n<pre><code class=\"language-python\"># \u7ed3\u6784\u5316\u65f6\u95f4\nnow_time = time.localtime()\nprint(now_time)<\/code><\/pre>\n<pre><code>time.struct_time(tm_year=2019, tm_mon=3, tm_mday=7, tm_hour=16, tm_min=22, tm_sec=11, tm_wday=3, tm_yday=66, tm_isdst=0)<\/code><\/pre>\n<pre><code class=\"language-python\"># \u628a\u7ed3\u6784\u5316\u65f6\u95f4\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u683c\u5f0f\nprint(time.mktime(now_time))<\/code><\/pre>\n<pre><code>1551946931.0<\/code><\/pre>\n<pre><code class=\"language-python\"># \u628a\u7ed3\u6784\u5316\u65f6\u95f4\u8f6c\u6362\u4e3a\u683c\u5f0f\u5316\u65f6\u95f4\n# %Y\u5e74-%m\u6708-%d\u5929 %X\u65f6\u5206\u79d2=%H\u65f6:%M\u5206:%S\u79d2\nprint(time.strftime(&quot;%Y-%m-%d %X&quot;, now_time))<\/code><\/pre>\n<pre><code>2019-03-07 16:22:11<\/code><\/pre>\n<pre><code class=\"language-python\"># \u628a\u683c\u5f0f\u5316\u65f6\u95f4\u5316\u4e3a\u7ed3\u6784\u5316\u65f6\u95f4\uff0c\u5b83\u548cstrftime()\u662f\u9006\u64cd\u4f5c\nprint(time.strptime(&#039;2013-05-20 13:14:52&#039;, &#039;%Y-%m-%d %X&#039;))<\/code><\/pre>\n<pre><code>time.struct_time(tm_year=2013, tm_mon=5, tm_mday=20, tm_hour=13, tm_min=14, tm_sec=52, tm_wday=0, tm_yday=140, tm_isdst=-1)<\/code><\/pre>\n<pre><code class=\"language-python\"># \u628a\u7ed3\u6784\u5316\u65f6\u95f4\u8868\u793a\u4e3a\u8fd9\u79cd\u5f62\u5f0f\uff1a&#039;Sun Jun 20 23:21:05 1993&#039;\u3002\nprint(time.asctime())<\/code><\/pre>\n<pre><code>Thu Mar  7 16:22:11 2019<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5982\u679c\u6ca1\u6709\u53c2\u6570\uff0c\u5c06\u4f1a\u5c06time.localtime()\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002\nprint(time.asctime(time.localtime()))<\/code><\/pre>\n<pre><code>Thu Mar  7 16:22:11 2019<\/code><\/pre>\n<pre><code class=\"language-python\"># \u628a\u4e00\u4e2a\u65f6\u95f4\u6233\u8f6c\u5316\u4e3atime.asctime()\u7684\u5f62\u5f0f\u3002\nprint(time.ctime())<\/code><\/pre>\n<pre><code>Thu Mar  7 16:22:11 2019<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5982\u679c\u53c2\u6570\u672a\u7ed9\u6216\u8005\u4e3aNone\u7684\u65f6\u5019\uff0c\u5c06\u4f1a\u9ed8\u8ba4time.time()\u4e3a\u53c2\u6570\u3002\u5b83\u7684\u4f5c\u7528\u76f8\u5f53\u4e8etime.asctime(time.localtime(secs))\u3002\nprint(time.ctime(time.time()))<\/code><\/pre>\n<pre><code>Thu Mar  7 16:22:11 2019<\/code><\/pre>\n<h3>\u5176\u4ed6\u7528\u6cd5<\/h3>\n<pre><code class=\"language-python\"># \u63a8\u8fdf\u6307\u5b9a\u7684\u65f6\u95f4\u8fd0\u884c\uff0c\u5355\u4f4d\u4e3a\u79d2\nstart = time.time()\ntime.sleep(3)\nend = time.time()\n\nprint(end-start)<\/code><\/pre>\n<pre><code>3.0005428791046143<\/code><\/pre>\n<h2>datetime\u6a21\u5757<\/h2>\n<pre><code class=\"language-python\"># datetime\u6a21\u5757\u53ef\u4ee5\u770b\u6210\u662f\u65f6\u95f4\u52a0\u51cf\u7684\u6a21\u5757\nimport datetime<\/code><\/pre>\n<pre><code class=\"language-python\"># \u8fd4\u56de\u5f53\u524d\u65f6\u95f4\nprint(datetime.datetime.now())<\/code><\/pre>\n<pre><code>2019-03-07 16:22:14.544130<\/code><\/pre>\n<pre><code class=\"language-python\">print(datetime.date.fromtimestamp(time.time()))<\/code><\/pre>\n<pre><code>2019-03-07<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5f53\u524d\u65f6\u95f4+3\u5929\nprint(datetime.datetime.now() + datetime.timedelta(3))<\/code><\/pre>\n<pre><code>2019-03-10 16:22:14.560599<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5f53\u524d\u65f6\u95f4-3\u5929\nprint(datetime.datetime.now() + datetime.timedelta(-3))<\/code><\/pre>\n<pre><code>2019-03-04 16:22:14.568473<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5f53\u524d\u65f6\u95f4-3\u5c0f\u65f6\nprint(datetime.datetime.now() + datetime.timedelta(hours=3))<\/code><\/pre>\n<pre><code>2019-03-07 19:22:14.575881<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5f53\u524d\u65f6\u95f4+30\u5206\u949f\nprint(datetime.datetime.now() + datetime.timedelta(minutes=30))<\/code><\/pre>\n<pre><code>2019-03-07 16:52:14.585432<\/code><\/pre>\n<pre><code class=\"language-python\"># \u65f6\u95f4\u66ff\u6362\nc_time = datetime.datetime.now()\nprint(c_time.replace(minute=20, hour=5, second=13))<\/code><\/pre>\n<pre><code>2019-03-07 05:20:13.595493<\/code><\/pre>\n<h2>random\u6a21\u5757<\/h2>\n<pre><code class=\"language-python\">import random<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5927\u4e8e0\u4e14\u5c0f\u4e8e1\u4e4b\u95f4\u7684\u5c0f\u6570\nprint(random.random())<\/code><\/pre>\n<pre><code>0.25435092120631386<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5927\u4e8e\u7b49\u4e8e1\u4e14\u5c0f\u4e8e\u7b49\u4e8e3\u4e4b\u95f4\u7684\u6574\u6570\nprint(random.randint(1, 3))<\/code><\/pre>\n<pre><code>2<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5927\u4e8e\u7b49\u4e8e1\u4e14\u5c0f\u4e8e3\u4e4b\u95f4\u7684\u6574\u6570\nprint(random.randrange(1, 3))<\/code><\/pre>\n<pre><code>1<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5927\u4e8e1\u5c0f\u4e8e3\u7684\u5c0f\u6570\uff0c\u59821.927109612082716\nprint(random.uniform(1, 3))<\/code><\/pre>\n<pre><code>2.718804989532962<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5217\u8868\u5185\u7684\u4efb\u610f\u4e00\u4e2a\u5143\u7d20\uff0c\u53731\u6216\u8005\u201823\u2019\u6216\u8005[4,5]\nprint(random.choice([1, &#039;23&#039;, [4, 5]]))<\/code><\/pre>\n<pre><code>1<\/code><\/pre>\n<pre><code class=\"language-python\"># random.sample([], n)\uff0c\u5217\u8868\u5143\u7d20\u4efb\u610fn\u4e2a\u5143\u7d20\u7684\u7ec4\u5408\uff0c\u793a\u4f8bn=2\nprint(random.sample([1, &#039;23&#039;, [4, 5]], 2))<\/code><\/pre>\n<pre><code>[[4, 5], '23']<\/code><\/pre>\n<pre><code class=\"language-python\">lis = [1, 3, 5, 7, 9]\n# \u6253\u4e71l\u7684\u987a\u5e8f,\u76f8\u5f53\u4e8e&quot;\u6d17\u724c&quot;\nrandom.shuffle(lis)\nprint(lis)<\/code><\/pre>\n<pre><code>[1, 3, 9, 7, 5]<\/code><\/pre>\n<h2>os\u6a21\u5757<\/h2>\n<p>&emsp;&emsp;os\u6a21\u5757\u8d1f\u8d23\u7a0b\u5e8f\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u4ea4\u4e92\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u65b9\u6cd5<\/th>\n<th style=\"text-align: center;\">\u8be6\u89e3<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">os.getcwd()<\/td>\n<td style=\"text-align: center;\">\u83b7\u53d6\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u5373\u5f53\u524dpython\u811a\u672c\u5de5\u4f5c\u7684\u76ee\u5f55\u8def\u5f84<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.chdir(&quot;dirname&quot;)<\/td>\n<td style=\"text-align: center;\">\u6539\u53d8\u5f53\u524d\u811a\u672c\u5de5\u4f5c\u76ee\u5f55\uff1b\u76f8\u5f53\u4e8eshell\u4e0bcd<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.curdir<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56de\u5f53\u524d\u76ee\u5f55: (&#8216;.&#8217;)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.pardir<\/td>\n<td style=\"text-align: center;\">\u83b7\u53d6\u5f53\u524d\u76ee\u5f55\u7684\u7236\u76ee\u5f55\u5b57\u7b26\u4e32\u540d\uff1a(&#8216;..&#8217;)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.makedirs(&#8216;dirname1\/dirname2&#8217;)<\/td>\n<td style=\"text-align: center;\">\u53ef\u751f\u6210\u591a\u5c42\u9012\u5f52\u76ee\u5f55<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.removedirs(&#8216;dirname1&#8217;)<\/td>\n<td style=\"text-align: center;\">\u82e5\u76ee\u5f55\u4e3a\u7a7a\uff0c\u5219\u5220\u9664\uff0c\u5e76\u9012\u5f52\u5230\u4e0a\u4e00\u7ea7\u76ee\u5f55\uff0c\u5982\u82e5\u4e5f\u4e3a\u7a7a\uff0c\u5219\u5220\u9664\uff0c\u4f9d\u6b64\u7c7b\u63a8<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.mkdir(&#8216;dirname&#8217;)<\/td>\n<td style=\"text-align: center;\">\u751f\u6210\u5355\u7ea7\u76ee\u5f55\uff1b\u76f8\u5f53\u4e8eshell\u4e2dmkdir dirname<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.rmdir(&#8216;dirname&#8217;)<\/td>\n<td style=\"text-align: center;\">\u5220\u9664\u5355\u7ea7\u7a7a\u76ee\u5f55\uff0c\u82e5\u76ee\u5f55\u4e0d\u4e3a\u7a7a\u5219\u65e0\u6cd5\u5220\u9664\uff0c\u62a5\u9519\uff1b\u76f8\u5f53\u4e8eshell\u4e2drmdir dirname<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.listdir(&#8216;dirname&#8217;)<\/td>\n<td style=\"text-align: center;\">\u5217\u51fa\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u5f55\uff0c\u5305\u62ec\u9690\u85cf\u6587\u4ef6\uff0c\u5e76\u4ee5\u5217\u8868\u65b9\u5f0f\u6253\u5370<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.remove()<\/td>\n<td style=\"text-align: center;\">\u5220\u9664\u4e00\u4e2a\u6587\u4ef6<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.rename(&quot;oldname&quot;,&quot;newname&quot;)<\/td>\n<td style=\"text-align: center;\">\u91cd\u547d\u540d\u6587\u4ef6\/\u76ee\u5f55<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.stat(&#8216;path\/filename&#8217;)<\/td>\n<td style=\"text-align: center;\">\u83b7\u53d6\u6587\u4ef6\/\u76ee\u5f55\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.sep<\/td>\n<td style=\"text-align: center;\">\u8f93\u51fa\u64cd\u4f5c\u7cfb\u7edf\u7279\u5b9a\u7684\u8def\u5f84\u5206\u9694\u7b26\uff0cwin\u4e0b\u4e3a&quot;\\&quot;,Linux\u4e0b\u4e3a&quot;\/&quot;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.linesep<\/td>\n<td style=\"text-align: center;\">\u8f93\u51fa\u5f53\u524d\u5e73\u53f0\u4f7f\u7528\u7684\u884c\u7ec8\u6b62\u7b26\uff0cwin\u4e0b\u4e3a&quot;\\t\\n&quot;,Linux\u4e0b\u4e3a&quot;\\n&quot;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.pathsep<\/td>\n<td style=\"text-align: center;\">\u8f93\u51fa\u7528\u4e8e\u5206\u5272\u6587\u4ef6\u8def\u5f84\u7684\u5b57\u7b26\u4e32 win\u4e0b\u4e3a;,Linux\u4e0b\u4e3a:<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.name<\/td>\n<td style=\"text-align: center;\">\u8f93\u51fa\u5b57\u7b26\u4e32\u6307\u793a\u5f53\u524d\u4f7f\u7528\u5e73\u53f0\u3002win-&gt;&#8217;nt&#8217;; Linux-&gt;&#8217;posix&#8217;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.system(&quot;bash command&quot;)<\/td>\n<td style=\"text-align: center;\">\u8fd0\u884cshell\u547d\u4ee4\uff0c\u76f4\u63a5\u663e\u793a<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.environ<\/td>\n<td style=\"text-align: center;\">\u83b7\u53d6\u7cfb\u7edf\u73af\u5883\u53d8\u91cf<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.path.abspath(path)<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56depath\u89c4\u8303\u5316\u7684\u7edd\u5bf9\u8def\u5f84<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.path.split(path)<\/td>\n<td style=\"text-align: center;\">\u5c06path\u5206\u5272\u6210\u76ee\u5f55\u548c\u6587\u4ef6\u540d\u4e8c\u5143\u7ec4\u8fd4\u56de<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.path.dirname(path)<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56depath\u7684\u76ee\u5f55\u3002\u5176\u5b9e\u5c31\u662fos.path.split(path)\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.path.basename(path)<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56depath\u6700\u540e\u7684\u6587\u4ef6\u540d\u3002\u5982\u4f55path\u4ee5\uff0f\u6216\\\u7ed3\u5c3e\uff0c\u90a3\u4e48\u5c31\u4f1a\u8fd4\u56de\u7a7a\u503c\u3002\u5373os.path.split(path)\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.path.exists(path)<\/td>\n<td style=\"text-align: center;\">\u5982\u679cpath\u5b58\u5728\uff0c\u8fd4\u56deTrue\uff1b\u5982\u679cpath\u4e0d\u5b58\u5728\uff0c\u8fd4\u56deFalse<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.path.isabs(path)<\/td>\n<td style=\"text-align: center;\">\u5982\u679cpath\u662f\u7edd\u5bf9\u8def\u5f84\uff0c\u8fd4\u56deTrue<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.path.isfile(path)<\/td>\n<td style=\"text-align: center;\">\u5982\u679cpath\u662f\u4e00\u4e2a\u5b58\u5728\u7684\u6587\u4ef6\uff0c\u8fd4\u56deTrue\u3002\u5426\u5219\u8fd4\u56deFalse<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.path.isdir(path)<\/td>\n<td style=\"text-align: center;\">\u5982\u679cpath\u662f\u4e00\u4e2a\u5b58\u5728\u7684\u76ee\u5f55\uff0c\u5219\u8fd4\u56deTrue\u3002\u5426\u5219\u8fd4\u56deFalse<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.path.join(path1[, path2[, &#8230;]])<\/td>\n<td style=\"text-align: center;\">\u5c06\u591a\u4e2a\u8def\u5f84\u7ec4\u5408\u540e\u8fd4\u56de\uff0c\u7b2c\u4e00\u4e2a\u7edd\u5bf9\u8def\u5f84\u4e4b\u524d\u7684\u53c2\u6570\u5c06\u88ab\u5ffd\u7565<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.path.getatime(path)<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56depath\u6240\u6307\u5411\u7684\u6587\u4ef6\u6216\u8005\u76ee\u5f55\u7684\u6700\u540e\u5b58\u53d6\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">os.path.getmtime(path)<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56depath\u6240\u6307\u5411\u7684\u6587\u4ef6\u6216\u8005\u76ee\u5f55\u7684\u6700\u540e\u4fee\u6539\u65f6\u95f4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>os.path.getsize(path) \u8fd4\u56depath\u7684\u5927\u5c0f<\/p>\n<h2>sys\u6a21\u5757<\/h2>\n<p>&emsp;&emsp;sys\u6a21\u5757\u8d1f\u8d23\u7a0b\u5e8f\u4e0ePython\u89e3\u91ca\u5668\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u65b9\u6cd5<\/th>\n<th style=\"text-align: center;\">\u8be6\u89e3<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">sys.argv<\/td>\n<td style=\"text-align: center;\">\u547d\u4ee4\u884c\u53c2\u6570List\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u7a0b\u5e8f\u672c\u8eab\u8def\u5f84<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.modules.keys()<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56de\u6240\u6709\u5df2\u7ecf\u5bfc\u5165\u7684\u6a21\u5757\u5217\u8868<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.exc_info()<\/td>\n<td style=\"text-align: center;\">\u83b7\u53d6\u5f53\u524d\u6b63\u5728\u5904\u7406\u7684\u5f02\u5e38\u7c7b,exc_type\u3001exc_value\u3001exc_traceback\u5f53\u524d\u5904\u7406\u7684\u5f02\u5e38\u8be6\u7ec6\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.exit(n)<\/td>\n<td style=\"text-align: center;\">\u9000\u51fa\u7a0b\u5e8f\uff0c\u6b63\u5e38\u9000\u51fa\u65f6exit(0)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.hexversion<\/td>\n<td style=\"text-align: center;\">\u83b7\u53d6Python\u89e3\u91ca\u7a0b\u5e8f\u7684\u7248\u672c\u503c\uff0c16\u8fdb\u5236\u683c\u5f0f\u5982\uff1a0x020403F0<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.version<\/td>\n<td style=\"text-align: center;\">\u83b7\u53d6Python\u89e3\u91ca\u7a0b\u5e8f\u7684\u7248\u672c\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.maxint<\/td>\n<td style=\"text-align: center;\">\u6700\u5927\u7684Int\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.maxunicode<\/td>\n<td style=\"text-align: center;\">\u6700\u5927\u7684Unicode\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.modules<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56de\u7cfb\u7edf\u5bfc\u5165\u7684\u6a21\u5757\u5b57\u6bb5\uff0ckey\u662f\u6a21\u5757\u540d\uff0cvalue\u662f\u6a21\u5757<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.path<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56de\u6a21\u5757\u7684\u641c\u7d22\u8def\u5f84\uff0c\u521d\u59cb\u5316\u65f6\u4f7f\u7528PYTHONPATH\u73af\u5883\u53d8\u91cf\u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.platform<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56de\u64cd\u4f5c\u7cfb\u7edf\u5e73\u53f0\u540d\u79f0<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.stdout<\/td>\n<td style=\"text-align: center;\">\u6807\u51c6\u8f93\u51fa<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.stdin<\/td>\n<td style=\"text-align: center;\">\u6807\u51c6\u8f93\u5165<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.stderr<\/td>\n<td style=\"text-align: center;\">\u9519\u8bef\u8f93\u51fa<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.exc_clear()<\/td>\n<td style=\"text-align: center;\">\u7528\u6765\u6e05\u9664\u5f53\u524d\u7ebf\u7a0b\u6240\u51fa\u73b0\u7684\u5f53\u524d\u7684\u6216\u6700\u8fd1\u7684\u9519\u8bef\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.exec_prefix<\/td>\n<td style=\"text-align: center;\">\u8fd4\u56de\u5e73\u53f0\u72ec\u7acb\u7684python\u6587\u4ef6\u5b89\u88c5\u7684\u4f4d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.byteorder<\/td>\n<td style=\"text-align: center;\">\u672c\u5730\u5b57\u8282\u89c4\u5219\u7684\u6307\u793a\u5668\uff0cbig-endian\u5e73\u53f0\u7684\u503c\u662f&#8217;big&#8217;,little-endian\u5e73\u53f0\u7684\u503c\u662f&#8217;little&#8217;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.copyright<\/td>\n<td style=\"text-align: center;\">\u8bb0\u5f55python\u7248\u6743\u76f8\u5173\u7684\u4e1c\u897f<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">sys.api_version<\/td>\n<td style=\"text-align: center;\">\u89e3\u91ca\u5668\u7684C\u7684API\u7248\u672c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>json\u548cpickle\u6a21\u5757<\/h2>\n<h3>\u5e8f\u5217\u5316<\/h3>\n<p>&emsp;&emsp;\u628a\u5bf9\u8c61(\u53d8\u91cf)\u4ece\u5185\u5b58\u4e2d\u53d8\u6210\u53ef\u5b58\u50a8\u6216\u4f20\u8f93\u7684\u8fc7\u7a0b\u79f0\u4e4b\u4e3a\u5e8f\u5217\u5316\uff0c\u5728Python\u4e2d\u53ebpickling\uff0c\u5728\u5176\u4ed6\u8bed\u8a00\u4e2d\u4e5f\u88ab\u79f0\u4e4b\u4e3aserialization\uff0cmarshalling\uff0cflattening\u3002<\/p>\n<p>\u5e8f\u5217\u5316\u7684\u4f18\u70b9\uff1a<\/p>\n<ol>\n<li>\u6301\u4e45\u4fdd\u5b58\u72b6\u6001\uff1a\u5185\u5b58\u662f\u65e0\u6cd5\u6c38\u4e45\u4fdd\u5b58\u6570\u636e\u7684\uff0c\u5f53\u7a0b\u5e8f\u8fd0\u884c\u4e86\u4e00\u6bb5\u65f6\u95f4\uff0c\u6211\u4eec\u65ad\u7535\u6216\u8005\u91cd\u542f\u7a0b\u5e8f\uff0c\u5185\u5b58\u4e2d\u5173\u4e8e\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u4e4b\u524d\u4e00\u6bb5\u65f6\u95f4\u7684\u6570\u636e\uff08\u6709\u7ed3\u6784\uff09\u90fd\u88ab\u6e05\u7a7a\u4e86\u3002\u4f46\u662f\u5728\u65ad\u7535\u6216\u91cd\u542f\u7a0b\u5e8f\u4e4b\u524d\u5c06\u7a0b\u5e8f\u5f53\u524d\u5185\u5b58\u4e2d\u6240\u6709\u7684\u6570\u636e\u90fd\u4fdd\u5b58\u4e0b\u6765\uff08\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff09\uff0c\u4ee5\u4fbf\u4e8e\u4e0b\u6b21\u7a0b\u5e8f\u6267\u884c\u80fd\u591f\u4ece\u6587\u4ef6\u4e2d\u8f7d\u5165\u4e4b\u524d\u7684\u6570\u636e\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c\uff0c\u8fd9\u5c31\u662f\u5e8f\u5217\u5316\u3002<\/li>\n<li>\u8de8\u5e73\u53f0\u6570\u636e\u4ea4\u4e92\uff1a\u5e8f\u5217\u5316\u65f6\u4e0d\u4ec5\u53ef\u4ee5\u628a\u5e8f\u5217\u5316\u540e\u7684\u5185\u5bb9\u5199\u5165\u78c1\u76d8\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u5230\u522b\u7684\u673a\u5668\u4e0a\uff0c\u5982\u679c\u6536\u53d1\u7684\u53cc\u65b9\u7ea6\u5b9a\u597d\u5b9e\u7528\u4e00\u79cd\u5e8f\u5217\u5316\u7684\u683c\u5f0f\uff0c\u90a3\u4e48\u4fbf\u6253\u7834\u4e86\u5e73\u53f0\/\u8bed\u8a00\u5dee\u5f02\u5316\u5e26\u6765\u7684\u9650\u5236\uff0c\u5b9e\u73b0\u4e86\u8de8\u5e73\u53f0\u6570\u636e\u4ea4\u4e92\u3002<\/li>\n<\/ol>\n<h3>json<\/h3>\n<p>&emsp;&emsp;Json\u5e8f\u5217\u5316\u5e76\u4e0d\u662fpython\u72ec\u6709\u7684\uff0cjson\u5e8f\u5217\u5316\u5728java\u7b49\u8bed\u8a00\u4e2d\u4e5f\u4f1a\u6d89\u53ca\u5230\uff0c\u56e0\u6b64\u4f7f\u7528json\u5e8f\u5217\u5316\u80fd\u591f\u8fbe\u5230\u8de8\u5e73\u53f0\u4f20\u8f93\u6570\u636e\u7684\u76ee\u7684\u3002<\/p>\n<p>json\u6570\u636e\u7c7b\u578b\u548cpython\u6570\u636e\u7c7b\u578b\u5bf9\u5e94\u5173\u7cfb\u8868<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">Json\u7c7b\u578b<\/th>\n<th style=\"text-align: center;\">Python\u7c7b\u578b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">{}<\/td>\n<td style=\"text-align: center;\">dict<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">[]<\/td>\n<td style=\"text-align: center;\">list<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">&quot;string&quot;<\/td>\n<td style=\"text-align: center;\">str<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">520.13<\/td>\n<td style=\"text-align: center;\">int\u6216float<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">true\/false<\/td>\n<td style=\"text-align: center;\">True\/False<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">null<\/td>\n<td style=\"text-align: center;\">None<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>json\u6a21\u5757\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u4e00\u4e2a\u8fc7\u7a0b\u5982\u4e0b\u56fe\u6240\u793a<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/03\/json\u6a21\u5757.jpg'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/03\/json\u6a21\u5757.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<pre><code class=\"language-python\">import json<\/code><\/pre>\n<pre><code class=\"language-python\">struct_data = {&#039;name&#039;: &#039;json&#039;, &#039;age&#039;: 23, &#039;sex&#039;: &#039;male&#039;}\nprint(struct_data, type(struct_data))<\/code><\/pre>\n<pre><code>{'name': 'json', 'age': 23, 'sex': 'male'} <class 'dict'><\/code><\/pre>\n<pre><code class=\"language-python\">data = json.dumps(struct_data)\nprint(data, type(data))<\/code><\/pre>\n<pre><code>{\"name\": \"json\", \"age\": 23, \"sex\": \"male\"} <class 'str'><\/code><\/pre>\n<pre><code class=\"language-python\"># \u6ce8\u610f\uff1a\u65e0\u8bba\u6570\u636e\u662f\u600e\u6837\u521b\u5efa\u7684\uff0c\u53ea\u8981\u6ee1\u8db3json\u683c\u5f0f\uff08\u5982\u679c\u662f\u5b57\u5178\uff0c\u5219\u5b57\u5178\u5185\u5143\u7d20\u90fd\u662f\u53cc\u5f15\u53f7\uff09\uff0c\u5c31\u53ef\u4ee5json.loads\u51fa\u6765,\u4e0d\u4e00\u5b9a\u975e\u8981dumps\u7684\u6570\u636e\u624d\u80fdloads\ndata = json.loads(data)\nprint(data, type(data))<\/code><\/pre>\n<pre><code>{'name': 'json', 'age': 23, 'sex': 'male'} <class 'dict'><\/code><\/pre>\n<pre><code class=\"language-python\"># \u5e8f\u5217\u5316\nwith open(&#039;Json\u5e8f\u5217\u5316\u5bf9\u8c61.json&#039;, &#039;w&#039;) as fw:\n    json.dump(struct_data, fw)<\/code><\/pre>\n<pre><code class=\"language-python\"># \u53cd\u5e8f\u5217\u5316\nwith open(&#039;Json\u5e8f\u5217\u5316\u5bf9\u8c61.json&#039;) as fr:\n    data = json.load(fr)\nprint(data)<\/code><\/pre>\n<pre><code>{'name': 'json', 'age': 23, 'sex': 'male'}<\/code><\/pre>\n<h3>pickle<\/h3>\n<p>&emsp;&emsp;Pickle\u5e8f\u5217\u5316\u548c\u6240\u6709\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u7279\u6709\u7684\u5e8f\u5217\u5316\u95ee\u9898\u4e00\u6837\uff0c\u5b83\u53ea\u80fd\u7528\u4e8ePython\uff0c\u5e76\u4e14\u53ef\u80fd\u4e0d\u540c\u7248\u672c\u7684Python\u5f7c\u6b64\u90fd\u4e0d\u517c\u5bb9\uff0c\u56e0\u6b64\uff0c\u53ea\u80fd\u7528Pickle\u4fdd\u5b58\u90a3\u4e9b\u4e0d\u91cd\u8981\u7684\u6570\u636e\uff0c\u5373\u4e0d\u80fd\u6210\u529f\u5730\u53cd\u5e8f\u5217\u5316\u4e5f\u6ca1\u5173\u7cfb\u3002\u4f46\u662fpickle\u7684\u597d\u5904\u662f\u53ef\u4ee5\u5b58\u50a8Python\u4e2d\u7684\u6240\u6709\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u5bf9\u8c61\uff0c\u800cjson\u4e0d\u53ef\u4ee5\u3002<\/p>\n<p>pickle\u6a21\u5757\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u8fc7\u7a0b\u5982\u4e0b\u56fe\u6240\u793a<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/03\/pickle\u6a21\u5757.jpg'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/03\/pickle\u6a21\u5757.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<pre><code class=\"language-python\">import pickle<\/code><\/pre>\n<pre><code class=\"language-python\">struct_data = {&#039;name&#039;: &#039;json&#039;, &#039;age&#039;: 23, &#039;sex&#039;: &#039;male&#039;}\nprint(struct_data, type(struct_data))<\/code><\/pre>\n<pre><code>{'name': 'json', 'age': 23, 'sex': 'male'} <class 'dict'><\/code><\/pre>\n<pre><code class=\"language-python\">data = pickle.dumps(struct_data)\nprint(data, type(data))<\/code><\/pre>\n<pre><code>b'\\x80\\x03}q\\x00(X\\x04\\x00\\x00\\x00nameq\\x01X\\x04\\x00\\x00\\x00jsonq\\x02X\\x03\\x00\\x00\\x00ageq\\x03K\\x17X\\x03\\x00\\x00\\x00sexq\\x04X\\x04\\x00\\x00\\x00maleq\\x05u.' <class 'bytes'><\/code><\/pre>\n<pre><code class=\"language-python\">data = pickle.loads(data)\nprint(data, type(data))<\/code><\/pre>\n<pre><code>{'name': 'json', 'age': 23, 'sex': 'male'} <class 'dict'><\/code><\/pre>\n<pre><code class=\"language-python\"># \u5e8f\u5217\u5316(\u6ce8\u610f\uff1apickle\u6a21\u5757\u9700\u8981\u4f7f\u7528\u4e8c\u8fdb\u5236\u5b58\u50a8\uff0c\u5373&#039;wb&#039;\u6a21\u5f0f\u5b58\u50a8)\nwith open(&#039;Pickle\u5e8f\u5217\u5316\u5bf9\u8c61.pkl&#039;, &#039;wb&#039;) as fw:\n    pickle.dump(struct_data, fw)<\/code><\/pre>\n<pre><code class=\"language-python\"># \u53cd\u5e8f\u5217\u5316\nwith open(&#039;Pickle\u5e8f\u5217\u5316\u5bf9\u8c61.pkl&#039;, &#039;rb&#039;) as fr:\n    pickle = pickle.load(fr)\nprint(data)<\/code><\/pre>\n<pre><code>{'name': 'json', 'age': 23, 'sex': 'male'}<\/code><\/pre>\n<h2>hashlib\u6a21\u5757<\/h2>\n<h3>hash\u662f\u4ec0\u4e48<\/h3>\n<p>&emsp;&emsp;hash\u662f\u4e00\u79cd\u7b97\u6cd5\uff08Python3.\u7248\u672c\u91cc\u4f7f\u7528hashlib\u6a21\u5757\u4ee3\u66ff\u4e86md5\u6a21\u5757\u548csha\u6a21\u5757\uff0c\u4e3b\u8981\u63d0\u4f9b SHA1\u3001SHA224\u3001SHA256\u3001SHA384\u3001SHA512\u3001MD5 \u7b97\u6cd5\uff09\uff0c\u8be5\u7b97\u6cd5\u63a5\u53d7\u4f20\u5165\u7684\u5185\u5bb9\uff0c\u7ecf\u8fc7\u8fd0\u7b97\u5f97\u5230\u4e00\u4e32hash\u503c\u3002<\/p>\n<p>hash\u503c\u7684\u7279\u70b9\uff1a<\/p>\n<ol>\n<li>\u53ea\u8981\u4f20\u5165\u7684\u5185\u5bb9\u4e00\u6837\uff0c\u5f97\u5230\u7684hash\u503c\u4e00\u6837\uff0c\u53ef\u7528\u4e8e\u975e\u660e\u6587\u5bc6\u7801\u4f20\u8f93\u65f6\u5bc6\u7801\u6821\u9a8c<\/li>\n<li>\u4e0d\u80fd\u7531hash\u503c\u8fd4\u89e3\u6210\u5185\u5bb9\uff0c\u5373\u53ef\u4ee5\u4fdd\u8bc1\u975e\u660e\u6587\u5bc6\u7801\u7684\u5b89\u5168\u6027<\/li>\n<li>\u53ea\u8981\u4f7f\u7528\u7684hash\u7b97\u6cd5\u4e0d\u53d8\uff0c\u65e0\u8bba\u6821\u9a8c\u7684\u5185\u5bb9\u6709\u591a\u5927\uff0c\u5f97\u5230\u7684hash\u503c\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u53ef\u4ee5\u7528\u4e8e\u5bf9\u6587\u672c\u7684\u54c8\u5e0c\u5904\u7406<\/li>\n<\/ol>\n<p>&emsp;&emsp;hash\u7b97\u6cd5\u5176\u5b9e\u53ef\u4ee5\u770b\u6210\u5982\u4e0b\u56fe\u6240\u793a\u7684\u4e00\u5ea7\u5de5\u5382\uff0c\u5de5\u5382\u63a5\u6536\u4f60\u9001\u6765\u7684\u539f\u6750\u6599\uff0c\u7ecf\u8fc7\u52a0\u5de5\u8fd4\u56de\u7684\u4ea7\u54c1\u5c31\u662fhash\u503c<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/03\/hashlib\u6a21\u5757.jpg'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/03\/hashlib\u6a21\u5757.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<pre><code class=\"language-python\">import hashlib<\/code><\/pre>\n<pre><code class=\"language-python\">m = hashlib.md5()\n\nm.update(&#039;hello&#039;.encode(&#039;utf8&#039;))\nprint(m.hexdigest())<\/code><\/pre>\n<pre><code>5d41402abc4b2a76b9719d911017c592<\/code><\/pre>\n<pre><code class=\"language-python\">m.update(&#039;hash&#039;.encode(&#039;utf8&#039;))\nprint(m.hexdigest())<\/code><\/pre>\n<pre><code>97fa850988687b8ceb12d773347f7712<\/code><\/pre>\n<pre><code class=\"language-python\">m2 = hashlib.md5()\nm2.update(&#039;hellohash&#039;.encode(&#039;utf8&#039;))\nprint(m2.hexdigest())<\/code><\/pre>\n<pre><code>97fa850988687b8ceb12d773347f7712<\/code><\/pre>\n<pre><code class=\"language-python\">m3 = hashlib.md5()\nm3.update(&#039;hello&#039;.encode(&#039;utf8&#039;))\nprint(m3.hexdigest())<\/code><\/pre>\n<pre><code>5d41402abc4b2a76b9719d911017c592<\/code><\/pre>\n<h3>\u649e\u5e93\u7834\u89e3hash\u7b97\u6cd5\u52a0\u5bc6<\/h3>\n<p>&emsp;&emsp;hash\u52a0\u5bc6\u7b97\u6cd5\u867d\u7136\u770b\u8d77\u6765\u5f88\u5389\u5bb3\uff0c\u4f46\u662f\u4ed6\u662f\u5b58\u5728\u4e00\u5b9a\u7f3a\u9677\u7684\uff0c\u5373\u53ef\u4ee5\u901a\u8fc7\u649e\u5e93\u53ef\u4ee5\u53cd\u89e3\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\u3002<\/p>\n<pre><code class=\"language-python\">import hashlib\n\n# \u5047\u5b9a\u6211\u4eec\u77e5\u9053hash\u7684\u5fae\u4fe1\u4f1a\u8bbe\u7f6e\u5982\u4e0b\u51e0\u4e2a\u5bc6\u7801\npwd_list = [\n    &#039;hash3714&#039;,\n    &#039;hash1313&#039;,\n    &#039;hash94139413&#039;,\n    &#039;hash123456&#039;,\n    &#039;123456hash&#039;,\n    &#039;h123ash&#039;,\n]\n\ndef make_pwd_dic(pwd_list):\n    dic = {}\n    for pwd in pwd_list:\n        m = hashlib.md5()\n        m.update(pwd.encode(&#039;utf-8&#039;))\n        dic[pwd] = m.hexdigest()\n    return dic\n\ndef break_code(hash_pwd, pwd_dic):\n    for k, v in pwd_dic.items():\n        if v == hash_pwd:\n            print(&#039;hash\u7684\u5fae\u4fe1\u7684\u5bc6\u7801\u662f===&gt;\\033[46m%s\\033[0m&#039; % k)\n\nhash_pwd = &#039;0562b36c3c5a3925dbe3c4d32a4f2ba2&#039;\nbreak_code(hash_pwd, make_pwd_dic(pwd_list))<\/code><\/pre>\n<pre><code>hash\u7684\u5fae\u4fe1\u7684\u5bc6\u7801\u662f===>\u001b[46mhash123456\u001b[0m<\/code><\/pre>\n<p>&emsp;&emsp;\u4e3a\u4e86\u9632\u6b62\u5bc6\u7801\u88ab\u649e\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528python\u4e2d\u7684\u53e6\u4e00\u4e2ahmac \u6a21\u5757\uff0c\u5b83\u5185\u90e8\u5bf9\u6211\u4eec\u521b\u5efakey\u548c\u5185\u5bb9\u505a\u8fc7\u67d0\u79cd\u5904\u7406\u540e\u518d\u52a0\u5bc6\u3002<\/p>\n<p>\u5982\u679c\u8981\u4fdd\u8bc1hmac\u6a21\u5757\u6700\u7ec8\u7ed3\u679c\u4e00\u81f4\uff0c\u5fc5\u987b\u4fdd\u8bc1\uff1a<\/p>\n<ol>\n<li>hmac.new\u62ec\u53f7\u5185\u6307\u5b9a\u7684\u521d\u59cbkey\u4e00\u6837<\/li>\n<li>\u65e0\u8bbaupdate\u591a\u5c11\u6b21\uff0c\u6821\u9a8c\u7684\u5185\u5bb9\u7d2f\u52a0\u5230\u4e00\u8d77\u662f\u4e00\u6837\u7684\u5185\u5bb9<\/li>\n<\/ol>\n<pre><code class=\"language-python\">import hmac\n\n# \u6ce8\u610fhmac\u6a21\u5757\u53ea\u63a5\u53d7\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u52a0\u5bc6\nh1 = hmac.new(b&#039;hash&#039;)\nh1.update(b&#039;hello&#039;)\nh1.update(b&#039;world&#039;)\nprint(h1.hexdigest())<\/code><\/pre>\n<pre><code>905f549c5722b5850d602862c34a763e<\/code><\/pre>\n<pre><code class=\"language-python\">h2 = hmac.new(b&#039;hash&#039;)\nh2.update(b&#039;helloworld&#039;)\nprint(h2.hexdigest())<\/code><\/pre>\n<pre><code>905f549c5722b5850d602862c34a763e<\/code><\/pre>\n<pre><code class=\"language-python\">h3 = hmac.new(b&#039;hashhelloworld&#039;)\nprint(h3.hexdigest())<\/code><\/pre>\n<pre><code>a7e524ade8ac5f7f33f3a39a8f63fd25<\/code><\/pre>\n<h2>logging\u6a21\u5757<\/h2>\n<p><\/p><div id=\"rml_readmorelogin_placeholder\" style=\"position:relative;\"><div id=\"rml_fade_content\" style=\"position: absolute;\r\ntop:-10em;\r\nwidth:100%;\r\nheight:10em;\r\nbackground: -webkit-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -moz-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -o-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -ms-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\"><\/div><div class=\"wpf-controller aru_rml_from_in_post\" style=\"background-color:#eeeeee;border:5px solid #cce6ff;\" id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><h2 id=\"Header\">\u67e5\u770b\u66f4\u591a<\/h2><div id=\"Message\"><p>\u8054\u7cfb\u7ba1\u7406\u5458\u5fae\u4fe1tutu19192010\uff0c\u6ce8\u518c\u8d26\u53f7<\/p>\n<\/div><div id=\"StatusBarHeader\"><\/div><form id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><input name=\"post_id\" value=\"4188\" type=\"hidden\"\/><input name=\"_init_callback\" value=\"InitLogin\" type=\"hidden\"\/><input name=\"post_id\" value=\"4188\" type=\"hidden\"\/><input name=\"rt_ype\" value=\"1\" type=\"hidden\"\/><input name=\"nonce\" value=\"f12167c8ee\" type=\"hidden\"\/><input name=\"_wpnonce\" value=\"a6be45eab3\" type=\"hidden\"\/><input name=\"_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_proxy_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_view\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginView\" type=\"hidden\"\/><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\" width=\"400px\"><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Username:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"username\" value=\"\" type=\"text\"\/><\/td><\/tr><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Password:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"password\" value=\"\" type=\"password\"\/><\/td><\/tr><\/tbody><\/table><p class=\"wpf-table-placeholder submit\"><button class=\"wp_plugin_framework_ajax_button\" type=\"button\" style=\"background-color:#4D90FE;;color:#ffffff;;border:1px solid #3079ed;\" name=\"_event\" value=\"ButtonLogin\">Log in<\/button><\/p><\/td><td class=\"wpf-table-placeholder-input\"><\/td><\/tr><\/tbody><\/table><\/form><div id=\"ButtonStartRegister\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=register\">Register<\/a><\/div><div id=\"Link1\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=lostpassword\">Forgotten username or password?<\/a><\/div><div id=\"StatusBarFooter\"><\/div><\/div><\/div><div id=aru_remaining_content><\/div>","protected":false},"excerpt":{"rendered":"<p>Python\u5e38\u7528\u6a21\u5757 time\u6a21\u5757 import time \u65f6\u95f4\u6233 &emsp;&emsp;\u65f6\u95f4\u6233(times [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":6705,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,281,327],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/4188"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4188"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/4188\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/media\/6705"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}