{"id":671,"date":"2017-03-15T21:34:57","date_gmt":"2017-03-15T13:34:57","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=600"},"modified":"2023-08-07T20:57:28","modified_gmt":"2023-08-07T12:57:28","slug":"python-article67","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/03\/15\/python-article67\/","title":{"rendered":"\u4f7f\u7528Python\u7f16\u5199\u7684\u6587\u4ef6\u5907\u4efd\u7a0b\u5e8f"},"content":{"rendered":"<p>\u4f7f\u7528Python\u7f16\u5199\u7684\u6587\u4ef6\u5907\u4efd\u7a0b\u5e8f<\/p>\n<h2>\u4f5c\u4e3a\u7a0b\u5e8f\u5458\u5e73\u65f6\u9700\u8981\u5907\u4efd\u8bb8\u591a\u4e1c\u897f\uff0c\u751f\u6015\u6709\u4e9b\u6587\u4ef6\u6539\u4e86\u5c31\u627e\u4e0d\u56de\u6765\u4e86\uff0c\u5c24\u5176\u662fU\u76d8\u91cc\u7684\u7ecf\u5e38\u4f1a\u88ab\u4fee\u6539\uff0c\u800c\u6211\u53c8\u4e0d\u80fd\u4e00\u4e2a\u6587\u4ef6\u4e00\u4e2a\u6587\u4ef6\u7684\u5bf9\u6bd4\u7740\u53bb\u5907\u4efd\uff0c\u5168\u590d\u5236\u53c8\u592a\u6d6a\u8d39\u65f6\u95f4\uff0c\u7f51\u4e0a\u5173\u4e8e\u5907\u4efd\u7684\u7a0b\u5e8f\u5f88\u591a\uff0c\u4f46\u6709\u5f88\u591a\u9650\u5236\uff0c\u6ca1\u6709\u6211\u60f3\u8981\u7684\u529f\u80fd\uff0c\u800c\u4e14\u6709\u7684\u8fd8\u4f1a\u6536\u8d39\uff0c\u6240\u4ee5\u51b3\u5b9a\u81ea\u5df1\u6765\u9020\u4e2a\u8f6e\u5b50\u3002<\/h2>\n<h2>\u5e73\u65f6\u7528PHP\u6bd4\u8f83\u591a\uff0c\u6211\u4e5f\u6709\u60f3\u8fc7\u7528PHP\u5199\u4e00\u4e2a\u7c7b\u4f3c\u7684\u6587\u4ef6\u6e38\u89c8\u5668\uff0c\u4f46\u8fd9\u4e2a\u8f6e\u5b50\u5c31\u9020\u7684\u6709\u4e9b\u5927\u4e86\uff0c\u6211\u53ea\u662f\u60f3\u81ea\u52a8\u5907\u4efdU\u76d8\u91cc\u7684\u4e1c\u897f\uff0c\u80fd\u591f\u81ea\u52a8\u5bf9\u6bd4\u5339\u914d\u76ee\u6807\u6587\u4ef6\u5c31\u53ef\u4ee5\uff0c\u4e0d\u9700\u8981\u592a\u81c3\u80bf\uff0c\u800cPython\u7b80\u5355\uff0c\u5feb\u901f\uff0c\u8fde\u754c\u9762\u90fd\u4e0d\u7528\u5199\uff0c\u6bcf\u6b21\u76f4\u63a5\u8fd0\u884c\u5c31\u884c\u3002\u6240\u4ee5\u6211\u51b3\u5b9a\u4f7f\u7528Python\u6765\u7f16\u5199\uff0c\u539f\u7406\u5c31\u662f\u5bf9\u5907\u4efd\u76ee\u5f55\u8fdb\u884c\u9012\u5f52\uff0c\u628a\u627e\u5230\u7684\u6587\u4ef6\u548c\u672c\u5730\u7684\u6587\u4ef6\u8fdb\u884c\u5bf9\u6bd4\uff0c\u5bf9\u6bd4\u65f6\u4f7f\u7528\u6587\u4ef6\u751f\u6210md5\u7801\u6765\u5339\u914d\uff0c\u5982\u679c\u672c\u5730\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u76ee\u5f55\uff0c\u518d\u590d\u5236\u6587\u4ef6\u3002\u5982\u679c\u6ca1\u6709Python\u73af\u5883\uff0c\u53ef\u4ee5\u76f4\u63a5\u4e0b\u8f7d\u6211\u7f16\u8bd1\u597d\u7684.exe\u7a0b\u5e8f\u76f4\u63a5\u4f7f\u7528\uff0c\u8f93\u5165\u65f6\u8981\u6ce8\u610f\uff0c\u7531\u4e8e\u6ca1\u6709\u5199\u56fe\u5f62\u754c\u9762\uff0c\u6587\u4ef6\u5939\u7684\u5730\u5740\u9700\u8981\u8f93\u5165\uff0c\u4e00\u5b9a\u8981\u4fdd\u8bc1\u6587\u4ef6\u5730\u5740\u7684\u6b63\u786e\uff0c\u5982\u679c\u662f\u4e3b\u76d8\u7b26\u5982d\u76d8\uff0c\u76f4\u63a5\u5199'd:',\\n\u5982\u679c\u662f\u6587\u4ef6\u5939\uff0c\u4f8b\u5982d\u76d8\u4e0b\u7684test\u6587\u4ef6\u5939\uff0c\u5219\u8f93\u5165d:\/test\u3002\u4e0b\u9762\u662f\u6e90\u4ee3\u7801\uff0c\u6ce8\u91ca\u5199\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u9700\u8981\u7684\u53ef\u4ee5\u6309\u7167\u81ea\u5df1\u7684\u9700\u6c42\u66f4\u6539\u3002<\/h2>\n<pre><code class=\"Python\">#-*- coding=utf8 -*-\n#\u4f7f\u7528\u51fd\u6570\u5bf9\u6307\u5b9a\u76ee\u5f55\u8fdb\u884c\u6df1\u5c42\u6b21\u904d\u5386\uff0c\u5148\u5217\u51fa\u6765\u6240\u6709\u7684\u6587\u4ef6\u548c\u76ee\u5f55\n#\u518d\u53bb\u6307\u5b9a\u7684\u76ee\u5f55\u91cc\u518d\u6b21\u67e5\u627e\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u4e00\u76f4\u6df1\u5165\u4e0b\u53bb\n# \u5982\u679c\u662f\u6587\u4ef6\uff0c\u5c31\u4e0d\u518d\u6df1\u5165\u4e0b\u53bb\uff0c\u800c\u53bb\u5224\u65ad\u8def\u5f84\u662f\u5426\u5b58\u5728\u4e8e\u7535\u8111\u4e2d\uff0c\n# \u4e0d\u5b58\u5728\u5219\u5c06\u5176\u590d\u5236\nimport hashlib\nimport os\nimport shutil\n#\u7528\u4e8e\u5224\u65ad\u4e24\u4e2a\u6587\u4ef6\u662f\u5426\u76f8\u540c\uff0c\u63d0\u53d6\u6bcf\u4e2a\u6587\u4ef6\u4e2d\u7684\u524d4\u5b57\u8282\u7684\u5185\u5bb9\u7136\u540e\u8f93\u51famd5\u7801\u8fdb\u884c\u6bd4\u8f83\ndef md5check(fname):\n    m = hashlib.md5()\n    with open(fname) as fobj:\n        while True:\n            data = fobj.read(4096)\n            if not data:\n                break\n            m.update(data)\n    return m.hexdigest()\n#\u6587\u4ef6\u590d\u5236\u7684\u51fd\u6570\ndef copy(path):\n    new_path = path\n    new_path = new_path.replace(old_scoure, new_scoure) #\u5c06\u539f\u76d8\u7b26\u66ff\u6362\u4e3a\u6211\u6240\u8981\u5b58\u653e\u7684\u76ee\u6807\u76d8\u7b26\n    print path + \"---copy---\" + new_path\n    if os.path.isfile(new_path): #\u5982\u679c\u76ee\u6807\u6587\u4ef6\u5b58\u5728\n        old_md = md5check(path)  #\u63d0\u53d6\u6e90\u6587\u4ef6\u7684md5\n        new_md = md5check(new_path)  #\u63d0\u53d6\u76ee\u6807\u6587\u4ef6\u7684md5\n        if old_md != new_md:  #\u5982\u679c\u4e0d\u76f8\u7b49\uff0c\u8bf4\u660e\u6e90\u6587\u4ef6\u6709\u4fee\u6539\n            shutil.copy(path, new_path) #\u56e0\u4e3a\u5df2\u7ecf\u5224\u65ad\u4e86\u76ee\u6807\u6587\u4ef6\u7684\u5b58\u5728\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u521b\u5efa\u76ee\u5f55\uff0c\u76f4\u63a5\u590d\u5236\u5c31\u53ef\u4ee5\n    else: #\u5982\u679c\u76ee\u6807\u6587\u4ef6\u4e0d\u5b58\u5728\n        dirname = os.path.dirname(new_path) # \u63d0\u53d6\u76ee\u6807\u7684\u76ee\u5f55\n        if os.path.exists(dirname): #\u76ee\u5f55\u5b58\u5728\n            shutil.copy(path, new_path) #\u76f4\u63a5\u590d\u5236\u6587\u4ef6\n        else: #\u76ee\u5f55\u4e0d\u5b58\u5728\n            try:\n                os.makedirs(dirname) #\u4f7f\u7528\u591a\u7ea7\u76ee\u5f55\u521b\u5efa\u51fd\u6570\u521b\u5efa\u76ee\u5f55\n                shutil.copy(path, new_path) #\u76f4\u63a5\u590d\u5236\u6587\u4ef6\n            except WindowsError: #\u9519\u8bef\u53cd\u9988\uff0c\u800c\u4e0d\u4f1a\u505c\u6b62\u64cd\u4f5c\n                print \"\u521b\u5efa\u76ee\u5f55\u51fa\u9519\"\n# \u67e5\u627e\u6240\u6709\u76ee\u5f55\u6587\u4ef6\u7684\u9012\u5f52\u51fd\u6570\ndef lsdir(forder):\n    path = os.listdir(forder) #\u5206\u79bb\u51fa\u6765\u7684\u6587\u4ef6\u5217\u8868\n    # print forder\n    # print path\n    for line in path: #\u9010\u4e2a\u5224\u65ad\n        line = forder + \"\/\" + line #\u6ca1\u6709\u4f7f\u7528join\u51fd\u6570\uff0c\u56e0\u4e3a\u4f1a\u51fa\u73b0 \\\n        if (os.path.isdir(line)): #\u662f\u76ee\u5f55\u5c31\u7ee7\u7eed\u8fdb\u884c\u9012\u5f52\n            lsdir(line)\n        else:\n            # path = os.path.join(forder,line)\n            # print 'l:'+line\n            copy(line) #\u5c06\u6587\u4ef6\u7684\u5730\u5740\u4f20\u5230copy\u51fd\u6570\u4e2d\u53bb\u590d\u5236\n        # break\ndef test():\n    print new_scoure\nif __name__=='__main__':\n    # lsdir('e:\/test')\n    global old_scoure\n    global new_scoure #\u8bbe\u7f6e\u4e3a\u5168\u5c40\u53d8\u91cf\n    concent = \"\u8bf7\u4fdd\u8bc1\u5730\u5740\u7684\u6b63\u786e\u6027\uff0c\u5982\u679c\u662f\u4e3b\u76d8\u7b26\u5982d\u76d8\uff0c\u76f4\u63a5\u5199'd:',\\n\u5982\u679c\u662f\u6587\u4ef6\u5939\uff0c\u4f8b\u5982d\u76d8\u4e0b\u7684test\u6587\u4ef6\u5939\uff0c\u5219\u8f93\u5165d:\/test\"\n    c_unicode = concent.decode(\"utf-8\") #\u4e3a\u4e86\u5728cmd\u663e\u793a\u4e2d\u6587\uff0c\u9700\u8981\u8fdb\u884c\u8f6c\u7801\n    # c_gbk = c_unicode.encode(\"gbk\")\n    print c_unicode\n    concent = \"\u8f93\u5165\u8981\u5907\u4efd\u7684\u6587\u4ef6\u5730\u5740\uff1a\"\n    c_unicode = concent.decode(\"utf-8\")\n    print c_unicode\n    old_scoure = raw_input()\n    concent = \"\u8f93\u5165\u5907\u4efd\u6587\u4ef6\u8981\u5b58\u653e\u7684\u5730\u5740,\u8bf7\u4fdd\u8bc1\u5730\u5740\u5b58\u5728\uff1a\"\n    c_unicode = concent.decode(\"utf-8\")\n    print c_unicode\n    new_scoure = raw_input()\n    # path = 'e:\/Markdown'\n    lsdir(old_scoure)\n    concent = \"\u8f93\u5165\u4efb\u610f\u952e\u5173\u95ed\"\n    c_unicode = concent.decode(\"utf-8\")\n    print c_unicode\n    raw_input()\n<\/code><\/pre>\n<h2><a href=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/test.exe\">\u4e0b\u8f7d\u5730\u5740http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/test.exe<\/a><\/h2>\n<h2>\u4e0b\u9762\u662f\u8fd0\u884c\u7684\u6548\u679c\u56fe\u7247<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog-1252406596.costj.myqcloud.com\/blog\/python40.jpg\" width=\"589\" height=\"287\" class=\"alignnone size-medium\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Python\u7f16\u5199\u7684\u6587\u4ef6\u5907\u4efd\u7a0b\u5e8f \u4f5c\u4e3a\u7a0b\u5e8f\u5458\u5e73\u65f6\u9700\u8981\u5907\u4efd\u8bb8\u591a\u4e1c\u897f\uff0c\u751f\u6015\u6709\u4e9b\u6587\u4ef6\u6539\u4e86\u5c31\u627e\u4e0d\u56de\u6765\u4e86\uff0c\u5c24\u5176\u662fU\u76d8\u91cc\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/03\/15\/python-article67\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":610,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[15],"tags":[58,76],"class_list":["post-671","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-python","tag-python","tag-io","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=671"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/671\/revisions"}],"predecessor-version":[{"id":1470,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/671\/revisions\/1470"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/610"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=671"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}