\u4e00\u3001shutil\u6a21\u5757\uff08\u4e86\u89e3\uff09\uff1a\u9ad8\u7ea7\u7684\u6587\u4ef6\u3001\u6587\u4ef6\u5939\u3001\u538b\u7f29\u5305\u5904\u7406\u6a21\u5757\u3002<\/strong><\/div>\n\r\nimport shutil\r\n \r\n# shutil.copyfileobj(fsrc, fdst[, length])\uff0c\u5c06\u6587\u4ef6\u5185\u5bb9\u62f7\u8d1d\u5230\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\r\nshutil.copyfileobj(open('old.xml', 'r'), open('new.xml', 'w'))\r\n \r\n# shutil.copyfile(src, dst)\uff0c\u62f7\u8d1d\u6587\u4ef6\r\nshutil.copyfile('f1.log', 'f2.log') # \u76ee\u6807\u6587\u4ef6\u65e0\u9700\u5b58\u5728\r\n \r\n# shutil.copymode(src, dst)\uff0c\u4ec5\u62f7\u8d1d\u6743\u9650\u3002\u5185\u5bb9\u3001\u7ec4\u3001\u7528\u6237\u5747\u4e0d\u53d8\r\nshutil.copymode('f1.log', 'f2.log') # \u76ee\u6807\u6587\u4ef6\u5fc5\u987b\u5b58\u5728\r\n \r\n# shutil.copystat(src, dst)\uff0c\u4ec5\u62f7\u8d1d\u72b6\u6001\u7684\u4fe1\u606f\uff0c\u5305\u62ec\uff1amode bits, atime, mtime, flags\r\nshutil.copystat('f1.log', 'f2.log') # \u76ee\u6807\u6587\u4ef6\u5fc5\u987b\u5b58\u5728\r\n \r\n# shutil.copy(src, dst)\uff0c\u62f7\u8d1d\u6587\u4ef6\u548c\u6743\u9650\r\nshutil.copy('f1.log', 'f2.log')\r\n \r\n# shutil.copy2(src, dst)\uff0c\u62f7\u8d1d\u6587\u4ef6\u548c\u72b6\u6001\u4fe1\u606f\r\nshutil.copy2('f1.log', 'f2.log')\r\n \r\n# shutil.ignore_patterns(*patterns)\r\n# shutil.copytree(src, dst, symlinks=False, ignore=None)\uff0c\u9012\u5f52\u7684\u53bb\u62f7\u8d1d\u6587\u4ef6\u5939\r\n# \u76ee\u6807\u76ee\u5f55\u4e0d\u80fd\u5b58\u5728\uff0c\u6ce8\u610f\u5bf9folder2\u76ee\u5f55\u7236\u7ea7\u76ee\u5f55\u8981\u6709\u53ef\u5199\u6743\u9650\uff0cignore\u7684\u610f\u601d\u662f\u6392\u9664\r\nshutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))\r\n \r\n# shutil.rmtree(path[, ignore_errors[, onerror]])\uff0c\u9012\u5f52\u7684\u53bb\u5220\u9664\u6587\u4ef6\r\nshutil.rmtree('folder1')\r\n \r\n# shutil.move(src, dst)\uff0c\u9012\u5f52\u7684\u53bb\u79fb\u52a8\u6587\u4ef6\uff0c\u5b83\u7c7b\u4f3cmv\u547d\u4ee4\uff0c\u5176\u5b9e\u5c31\u662f\u91cd\u547d\u540d\r\nshutil.move('folder1', 'folder3')\r\n \r\n# shutil.make_archive(base_name, format, ...)\uff0c\u521b\u5efa\u538b\u7f29\u5305\u5e76\u8fd4\u56de\u6587\u4ef6\u8def\u5f84\uff0c\u4f8b\u5982\uff1azip\u3001tar\r\n'''\r\nbase_name\uff1a \u538b\u7f29\u5305\u7684\u6587\u4ef6\u540d\uff0c\u4e5f\u53ef\u4ee5\u662f\u538b\u7f29\u5305\u7684\u8def\u5f84\u3002\u53ea\u662f\u6587\u4ef6\u540d\u65f6\uff0c\u5219\u4fdd\u5b58\u81f3\u5f53\u524d\u76ee\u5f55\uff0c\u5426\u5219\u4fdd\u5b58\u81f3\u6307\u5b9a\u8def\u5f84\uff0c\u5982 data_bak = >\u4fdd\u5b58\u81f3\u5f53\u524d\u8def\u5f84\uff1b\/ tmp\/data_bak = >\u4fdd\u5b58\u81f3\/tmp\/\r\nformat\uff1a\u538b\u7f29\u5305\u79cd\u7c7b\uff0c\u201czip\u201d, \u201ctar\u201d, \u201cbztar\u201d\uff0c\u201cgztar\u201d\r\nroot_dir\uff1a\u8981\u538b\u7f29\u7684\u6587\u4ef6\u5939\u8def\u5f84\uff08\u9ed8\u8ba4\u5f53\u524d\u76ee\u5f55\uff09\r\nowner\uff1a\u7528\u6237\uff0c\u9ed8\u8ba4\u5f53\u524d\u7528\u6237\r\ngroup\uff1a\u7ec4\uff0c\u9ed8\u8ba4\u5f53\u524d\u7ec4\r\nlogger\uff1a\u7528\u4e8e\u8bb0\u5f55\u65e5\u5fd7\uff0c\u901a\u5e38\u662flogging.Logger\u5bf9\u8c61\r\n'''\r\n \r\n# \u5c06 \/data \u4e0b\u7684\u6587\u4ef6\u6253\u5305\u653e\u7f6e\u5f53\u524d\u7a0b\u5e8f\u76ee\u5f55\r\nret = shutil.make_archive(\"data_bak\", 'gztar', root_dir='\/data')\r\n \r\n# \u5c06 \/data\u4e0b\u7684\u6587\u4ef6\u6253\u5305\u653e\u7f6e \/tmp\/\u76ee\u5f55\r\nret = shutil.make_archive(\"\/tmp\/data_bak\", 'gztar', root_dir='\/data')<\/pre>\nshutil \u5bf9\u538b\u7f29\u5305\u7684\u5904\u7406\u662f\u8c03\u7528 ZipFile \u548c TarFile \u4e24\u4e2a\u6a21\u5757\u6765\u8fdb\u884c\u7684\uff0c\u8be6\u7ec6\uff1a<\/p>\n
1\u3001 zipfile\u538b\u7f29\u89e3\u538b\u7f29<\/strong><\/span><\/div>\n\r\nimport zipfile\r\n \r\n# \u538b\u7f29\r\nz = zipfile.ZipFile('laxi.zip', 'w')\r\nz.write('a.log')\r\nz.write('data.data')\r\nz.close()\r\n \r\n# \u89e3\u538b\r\nz = zipfile.ZipFile('laxi.zip', 'r')\r\nz.extractall(path='.')\r\nz.close()<\/pre>\n2\u3001 tarfile\u538b\u7f29\u89e3\u538b\u7f29<\/strong><\/span><\/div>\n\r\nimport tarfile\r\n \r\n# \u538b\u7f29\r\nt=tarfile.open('\/tmp\/egon.tar','w')\r\nt.add('\/test1\/a.py',arcname='a.bak')\r\nt.add('\/test1\/b.py',arcname='b.bak')\r\nt.close()\r\n \r\n \r\n# \u89e3\u538b\r\nt=tarfile.open('\/tmp\/egon.tar','r')\r\nt.extractall('\/egon')\r\nt.close()<\/pre>\n\u4e8c\u3001subprocess\u6a21\u5757(\u4e86\u89e3)\uff1a\u8fd0\u884c\u5b50\u7a0b\u5e8f<\/strong><\/div>\nsubprocess\u6a21\u5757\u5141\u8bb8\u4f60\u53bb\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u8ba9\u5176\u6267\u884c\u53e6\u5916\u7684\u7a0b\u5e8f\uff0c\u5e76\u4e0e\u5b83\u8fdb\u884c\u901a\u4fe1\uff0c\u83b7\u53d6\u6807\u51c6\u7684\u8f93\u5165\u3001\u6807\u51c6\u8f93\u51fa\u3001\u6807\u51c6\u9519\u8bef\u4ee5\u53ca\u8fd4\u56de\u7801\u7b49\u3002<\/p>\n
\u66f4\u591a\u67e5\u770b\u5b98\u7f51\uff1ahttps:\/\/docs.python.org\/2\/library\/subprocess.html?highlight=subprocess#frequently-used-arguments<\/p>\n
\r\nimport subprocess\r\n'''\r\nsh-3.2# ls \/Users\/nick\/Desktop |grep txt$\r\nmysql.txt\r\ntt.txt\r\n\u4e8b\u7269.txt\r\n'''\r\n \r\nres1 = subprocess.Popen('ls \/Users\/jieli\/Desktop',shell=True, stdout=subprocess.PIPE)\r\nres = subprocess.Popen('grep txt$', shell=True,stdin=res1.stdout, stdout=subprocess.PIPE)\r\nprint(res.stdout.read().decode('utf-8'))\r\n \r\n# \u4e0b\u9762\u8fd9\u6bb5\u7b49\u540c\u4e8e\u4e0a\u9762,\u4f46\u662f\u4e0a\u9762\u7684\u4f18\u52bf\u5728\u4e8e,\u4e00\u4e2a\u6570\u636e\u6d41\u53ef\u4ee5\u548c\u53e6\u5916\u4e00\u4e2a\u6570\u636e\u6d41\u4ea4\u4e92,\u53ef\u4ee5\u901a\u8fc7\u722c\u866b\u5f97\u5230\u7ed3\u679c\u7136\u540e\u4ea4\u7ed9grep\u3002\r\nres1 = subprocess.Popen('ls \/Users\/jieli\/Desktop |grep txt$',shell=True, stdout=subprocess.PIPE)\r\nprint(res1.stdout.read().decode('utf-8'))\r\n \r\n# windows\u4e0b:\r\n# dir | findstr 'test*'\r\n# dir | findstr 'txt$'\r\nres1 = subprocess.Popen(r'dir \u201cC:\\Users\\Administrator\\PycharmProjects\\test\\\u51fd\u6570\u5907\u8bfe\u201d', shell=True, stdout=subprocess.PIPE)\r\nres = subprocess.Popen('findstr test*', shell=True, stdin=res1.stdout, stdout=subprocess.PIPE)\r\n \r\n# subprocess\u4f7f\u7528\u5f53\u524d\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\uff0c\u5f97\u5230\u7ed3\u679c\u4e3abytes\u7c7b\u578b\uff0c\u5728windows\u4e0b\u9700\u8981\u7528gbk\u89e3\u7801\r\nprint(res.stdout.read().decode('gbk') )\r\nimport subprocess\r\n'''\r\nsh-3.2# ls \/Users\/nick\/Desktop |grep txt$\r\nmysql.txt\r\ntt.txt\r\n\u4e8b\u7269.txt\r\n'''\r\n\r\nres1 = subprocess.Popen('ls \/Users\/jieli\/Desktop',shell=True, stdout=subprocess.PIPE)\r\nres = subprocess.Popen('grep txt$', shell=True,stdin=res1.stdout, stdout=subprocess.PIPE)\r\nprint(res.stdout.read().decode('utf-8'))\r\n\r\n# \u4e0b\u9762\u8fd9\u6bb5\u7b49\u540c\u4e8e\u4e0a\u9762,\u4f46\u662f\u4e0a\u9762\u7684\u4f18\u52bf\u5728\u4e8e,\u4e00\u4e2a\u6570\u636e\u6d41\u53ef\u4ee5\u548c\u53e6\u5916\u4e00\u4e2a\u6570\u636e\u6d41\u4ea4\u4e92,\u53ef\u4ee5\u901a\u8fc7\u722c\u866b\u5f97\u5230\u7ed3\u679c\u7136\u540e\u4ea4\u7ed9grep\u3002\r\nres1 = subprocess.Popen('ls \/Users\/jieli\/Desktop |grep txt$',shell=True, stdout=subprocess.PIPE)\r\nprint(res1.stdout.read().decode('utf-8'))\r\n\r\n# windows\u4e0b:\r\n# dir | findstr 'test*'\r\n# dir | findstr 'txt$'\r\nres1 = subprocess.Popen(r'dir \u201cC:\\Users\\Administrator\\PycharmProjects\\test\\\u51fd\u6570\u5907\u8bfe\u201d', shell=True, stdout=subprocess.PIPE)\r\nres = subprocess.Popen('findstr test*', shell=True, stdin=res1.stdout, stdout=subprocess.PIPE)\r\n\r\n# subprocess\u4f7f\u7528\u5f53\u524d\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\uff0c\u5f97\u5230\u7ed3\u679c\u4e3abytes\u7c7b\u578b\uff0c\u5728windows\u4e0b\u9700\u8981\u7528gbk\u89e3\u7801\r\nprint(res.stdout.read().decode('gbk') )<\/pre>\n\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8ePython\u4f7f\u7528shutil\u64cd\u4f5c\u6587\u4ef6\u3001subprocess\u8fd0\u884c\u5b50\u7a0b\u5e8f\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
import shutil # shutil.copyfileobj(fsrc, fdst[, length] […]<\/p>\n","protected":false},"author":362,"featured_media":231815,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-243891","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=243891"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243891\/revisions"}],"predecessor-version":[{"id":243894,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243891\/revisions\/243894"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/231815"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=243891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=243891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=243891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}