{"id":1764,"date":"2020-07-01T04:31:26","date_gmt":"2020-06-30T20:31:26","guid":{"rendered":"http:\/\/39.97.231.10\/?p=1764"},"modified":"2020-07-01T04:31:26","modified_gmt":"2020-06-30T20:31:26","slug":"python%e8%a7%a3%e5%8e%8b%e7%bc%a9%e6%96%87%e4%bb%b6%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=1764","title":{"rendered":"Python\u89e3\u538b\u7f29\u6587\u4ef6\u65b9\u6cd5"},"content":{"rendered":"<p>\u5de5\u4f5c\u4e2d\u7ecf\u5e38\u9047\u5230\u9700\u8981\u89e3\u538b\u5404\u79cd\u538b\u7f29\u5305\u7684\u63a5\u53e3\u9700\u6c42\uff0c\u5305\u62ec.tar\u3001.tar.gz\u3001.zip\u3001.rar\u5305\uff0c\u4e3a\u4e86\u907f\u514d\u6bcf\u6b21\u90fd\u5f97\u91cd\u65b0\u8c37\u6b4c\uff0c\u8bd5\u9519\u8c03\u9519\uff0c\u6211\u5c06\u5404\u79cd\u89e3\u538b\u7684python\u4ee3\u7801\u6574\u7406\u51fa\u6765\uff0c\u4e0b\u6b21\u53ef\u4ee5\u76f4\u63a5\u62ff\u8fc7\u6765\u7528\uff0c\u5efa\u8bae\u5c0f\u4f19\u4f34\u4eec\u6536\u85cf\u3002<\/p>\n<article>\n<div>\n<pre>import tarfile\nimport zipfile\nimport rarfile\ndef uncompress(src_file, dest_dir):\n    \\\"\\\"\\\"\u89e3\u538b\u5404\u79cd\u7c7b\u578b\u7684\u538b\u7f29\u5305\n    :param src_file: \u4f60\u8981\u89e3\u538b\u7684\u538b\u7f29\u5305\u6587\u4ef6\n    :type src_file: file\n    :param dest_dir: \u4f60\u8981\u89e3\u538b\u5230\u7684\u76ee\u6807\u8def\u5f84\n    :type dest_dir: str\n    \\\"\\\"\\\"\n    file_name, file_type = os.path.splitext(src_file.filename)\n    try:\n        if file_type == \\'.zip\\':\n            # \u9700\u8981\u5b89\u88c5zip\u5305\uff1apip install zipp\n            zip_file = zipfile.ZipFile(src_file)\n            for names in zip_file.namelist():\n                zip_file.extract(names, dest_dir)\n            zip_file.close()\n        elif file_type == \\'.rar\\':\n            # \u9700\u8981\u5b89\u88c5rar\u5305\uff1apip install rarfile\n            rar = rarfile.RarFile(src_file)\n            os.chdir(dest_dir)\n            rar.extractall()\n            rar.close()\n        else:\n            # file_type == \\'.tgz\\' or file_type == \\'.tar\\' or file_type == \\'.gz\\'\n            # Python\u81ea\u5e26tarfile\u6a21\u5757\n            tar = tarfile.open(fileobj=src_file)\n            for name in tar.getnames():\n                tar.extract(name, dest_dir)\n            tar.close()\n    except Exception as ex:\n        return False\n    return True\n<\/pre>\n<\/div>\n<p>\u8c03\u7528\u793a\u4f8b\uff1a<\/p>\n<div>\n<pre>if __name__ == \\'__main__\\':\n    dest_dir = \\'\u4f60\u8981\u89e3\u538b\u5230\u7684\u76ee\u6807\u8def\u5f84\\'\n    with open(\\'\u4f60\u8981\u89e3\u538b\u7684\u538b\u7f29\u5305\u6587\u4ef6\u8def\u5f84\\', \\'rb\\') as src_file:\n        result = uncompress(src_file, dest_dir)\n<\/pre>\n<\/div>\n<p>\u6ce8\u610f\uff1aliunx\u89e3\u538brar\u5305\uff0c\u9700\u8981\u5b89\u88c5unrar\uff1a<\/p>\n<div>\n<pre>$ sudo apt-get install unrar<\/pre>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u5de5\u4f5c\u4e2d\u7ecf\u5e38\u9047\u5230\u9700\u8981\u89e3\u538b\u5404\u79cd\u538b\u7f29\u5305\u7684\u63a5\u53e3\u9700\u6c42\uff0c\u5305\u62ec.tar\u3001.tar.gz\u3001.zip\u3001.rar\u5305\uff0c\u4e3a\u4e86\u907f\u514d\u6bcf\u6b21\u90fd &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=1764\"> <span class=\"screen-reader-text\">Python\u89e3\u538b\u7f29\u6587\u4ef6\u65b9\u6cd5<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[106,126],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"\u5de5\u4f5c\u4e2d\u7ecf\u5e38\u9047\u5230\u9700\u8981\u89e3\u538b\u5404\u79cd\u538b\u7f29\u5305\u7684\u63a5\u53e3\u9700\u6c42\uff0c\u5305\u62ec.tar\u3001.tar.gz\u3001.zip\u3001.rar\u5305\uff0c\u4e3a\u4e86\u907f\u514d\u6bcf\u6b21\u90fd&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1764"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1764"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1764\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1764"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}