{"id":405,"date":"2019-02-28T16:44:04","date_gmt":"2019-02-28T08:44:04","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=405"},"modified":"2022-05-17T09:43:04","modified_gmt":"2022-05-17T01:43:04","slug":"python-2","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=405","title":{"rendered":"python\u5e38\u7528\u7684\u51e0\u79cd\u6587\u4ef6\u64cd\u4f5c\uff0c\u590d\u5236\uff0c\u79fb\u52a8\uff0c\u6253\u5305"},"content":{"rendered":"<h1>\u9700\u6c42<\/h1>\n<p>\u7ecf\u5e38\u4f1a\u6709\u7528\u5230python\u7684\u6587\u4ef6\u590d\u5236\uff0c\u79fb\u52a8\uff0c\u6253\u5305\u7684\u529f\u80fd\uff0c \u6211\u8fd9\u8fb9\u7528\u7684\u65b9\u6cd5\u662fshuil\u5e93\u6765\u8fdb\u884c\u8fd9\u4e9b\u64cd\u4f5c\u3002\u5076\u5c14\u7a7f\u63d2\u4e86\u4e00\u4e9b\u56fe\u7247\u5927\u5c0f\u91cd\u7f6epil\u5e93\u548c\u6c49\u5b57\u8f6c\u62fc\u97f3\u7684\u65b9\u6cd5pypinyin\u5e93<\/p>\n<h1>\u4ee3\u7801<\/h1>\n<pre class=\"lang:python decode:true  EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\"># -*- coding: UTF-8 -*-\nimport os\nimport shutil\nimport traceback\nimport pypinyin\nfrom PIL import Image\nfrom globalLog import ta_log\ndef copy_file(srcfile, dstfile):\n    if not os.path.isfile(srcfile):\n        # \u4e00\u822c\u6765\u8bf4\uff0c\u662f\u56e0\u4e3a\u590d\u5236\u7684\u4e0d\u662f\u6587\u4ef6\u6240\u5bfc\u81f4\u7684,\u4e0d\u5f71\u54cd\n        ta_log.info(\"%s not file!\" % (srcfile))\n    else:\n        fpath, fname = os.path.split(dstfile)  # \u5206\u79bb\u6587\u4ef6\u540d\u548c\u8def\u5f84\n        if not os.path.exists(fpath):\n            os.makedirs(fpath)  # \u521b\u5efa\u8def\u5f84\n        shutil.copy2(srcfile, dstfile)  # \u79fb\u52a8\u6587\u4ef6\n        ta_log.info(\"copy %s -&gt; %s\" % (srcfile, dstfile))\ndef movefile(srcfile, dstfile):\n    if not os.path.isfile(srcfile):\n        ta_log.error(\"%s not exist!\" % (srcfile))\n    else:\n        fpath, fname = os.path.split(dstfile)  # \u5206\u79bb\u6587\u4ef6\u540d\u548c\u8def\u5f84\n        if not os.path.exists(fpath):\n            os.makedirs(fpath)  # \u521b\u5efa\u8def\u5f84\n        shutil.move(srcfile, dstfile)  # \u79fb\u52a8\u6587\u4ef6\n        ta_log.info(\"move %s -&gt; %s\" % (srcfile, dstfile))\ndef copy_file_to_capture(srcfile):\n    '''\n    \u590d\u5236\u5230capture\u76ee\u5f55\uff0c\u5e76\u4e14\u628a\u6c49\u5b57\u6539\u6210\u62fc\u97f3\n    :param srcfile:\n    :return:\n    '''\n    fpath, fname = os.path.split(srcfile)\n    filename_pre_ = pypinyin.slug(fname)\n    dstfile = os.path.abspath(os.getcwd() + \"\/filepath\/attachement\/capture\/\" + filename_pre_)\n    copy_file(srcfile, dstfile)\ndef resize_mage(filein):\n    img = Image.open(filein)\n    out = img.resize((129, 149), Image.ANTIALIAS)  # resize image with high-quality\n    out.save(filein, 'png')\n# \u53ea\u80fd\u7528\u4e8e\u5220\u9664\u672c\u5730\u6587\u4ef6\ndef delete_many_file(file_list):\n    for path in file_list:\n        if os.path.exists(path):\n            # \u5220\u9664\u6587\u4ef6\uff0c\u53ef\u4f7f\u7528\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\u3002\n            try:\n                os.remove(path)\n            except Exception:\n                ta_log.error(traceback.format_exc())\n            # os.unlink(my_file)\ndef copy_and_zip(file_list, dst_folder_name):\n    '''\n    \u6279\u91cf\u590d\u5236\u6587\u4ef6\u5230\u6307\u5b9a\u6587\u4ef6\u5939\uff0c\u7136\u540e\u628a\u6307\u5b9a\u6587\u4ef6\u5939\u7684\u5185\u5bb9\u538b\u7f29\u6210ZIP\u5e76\u4e14\u5220\u6389\u8be5\u6587\u4ef6\u5939\n    :param file_list: \u6587\u4ef6\u6216\u6587\u4ef6\u5939\n    :param dst_folder_name: \u76ee\u6807\u538b\u7f29\u6587\u4ef6\u7684\u540d\u79f0\n    :return:\n    '''\n    for item in file_list:\n        copy_files_to_attachment(item, dst_folder_name)\n    source = os.getcwd() + \"\/filepath\/attachement\/\" + dst_folder_name\n    shutil.make_archive(source, \"zip\", source)\n    shutil.rmtree(source)\ndef copy_files_to_attachment(srcfile, filename):\n    '''\n    \u628a\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u590d\u5236\u5230\u6307\u5b9a\u76ee\u5f55\u4e2d\n    :param srcfile: \u6587\u4ef6\u6216\u8005\u6587\u4ef6\u5939\u7684\u7edd\u5bf9\u8def\u5f84\n    :param filename: \u6307\u5b9a\u76ee\u5f55\n    :return:\n    '''\n    dstfile = os.path.abspath(os.getcwd() + \"\/filepath\/attachement\/\")\n    folder_name = dstfile + \"\\\\\" + filename + \"\\\\\"\n    if not os.path.isfile(srcfile):\n        last_name = os.path.basename(srcfile)\n        destination_name = folder_name + last_name\n        shutil.copytree(srcfile, destination_name, symlinks=True)\n    else:\n        fpath, fname = os.path.split(folder_name)  # \u5206\u79bb\u6587\u4ef6\u540d\u548c\u8def\u5f84\n        if not os.path.exists(fpath):\n            os.makedirs(fpath)  # \u521b\u5efa\u8def\u5f84\n        shutil.copy2(srcfile, folder_name)  # \u79fb\u52a8\u6587\u4ef6\n        print(\"copy %s -&gt; %s\" % (srcfile, folder_name))\ndef copy_file_to_folder(srcfile, folder_name):\n    '''\n       \u628a\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u590d\u5236\u5230\u6307\u5b9a\u76ee\u5f55\u4e2d\n       :param srcfile: \u6587\u4ef6\u6216\u8005\u6587\u4ef6\u5939\u7684\u7edd\u5bf9\u8def\u5f84\n       :param filename: \u6307\u5b9a\u76ee\u5f55\n       :return:\n       '''\n    if not os.path.isfile(srcfile):\n        last_name = os.path.basename(srcfile)\n        destination_name = folder_name + last_name\n        shutil.copytree(srcfile, destination_name, symlinks=True)\n    else:\n        fpath, fname = os.path.split(folder_name)  # \u5206\u79bb\u6587\u4ef6\u540d\u548c\u8def\u5f84\n        if not os.path.exists(fpath):\n            os.makedirs(fpath)  # \u521b\u5efa\u8def\u5f84\n        shutil.copy2(srcfile, folder_name)  # \u79fb\u52a8\u6587\u4ef6\n        print(\"copy %s -&gt; %s\" % (srcfile, folder_name))\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42 \u7ecf\u5e38\u4f1a\u6709\u7528\u5230python\u7684\u6587\u4ef6\u590d\u5236\uff0c\u79fb\u52a8\uff0c\u6253\u5305\u7684\u529f\u80fd\uff0c \u6211\u8fd9\u8fb9\u7528\u7684\u65b9\u6cd5\u662fshuil\u5e93\u6765\u8fdb\u884c\u8fd9\u4e9b\u64cd\u4f5c\u3002\u5076\u5c14 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-405","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=405"}],"version-history":[{"count":1,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/405\/revisions"}],"predecessor-version":[{"id":917,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/405\/revisions\/917"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}