{"id":54,"date":"2014-06-23T14:49:43","date_gmt":"2014-06-23T14:49:43","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=54"},"modified":"2014-06-23T14:49:43","modified_gmt":"2014-06-23T14:49:43","slug":"%e7%94%a8python%e5%86%99%e7%9a%84%e6%96%87%e4%bb%b6%e5%88%86%e5%89%b2%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/54.html","title":{"rendered":"\u7528Python\u5199\u7684\u6587\u4ef6\u5206\u5272\u811a\u672c"},"content":{"rendered":"<h6>\u7528Python\u5199\u7684\u5927\u6587\u4ef6\u5206\u5272\u811a\u672c\uff1a<\/h6>\n<pre class=\"lang:python decode:true \">#!\/usr\/bin\/env python\n#-*- coding: UTF-8 -*-\n\nimport sys\nfrom os.path import exists\nfileCount = 0\n\ndef splitFile(fileName, partSize=1):\n\t# 1024 * 1024 = 1048576\n\tlength = partSize * 1048576\n\tf1 = open(fileName, \"rb\")\n\twhile True:\n\t\tcontent = f1.read(length)\n\t\tif content == \"\":\n\t\t\tbreak\n\t\tnewFile = distFile(fileName)\n\t\tf2 = open(newFile, \"wb\")\n\t\tf2.write(content)\n\t\tf2.close()\n\tf1.close()\n\tprint 'split file complete!'\n\ndef distFile(sourceFile):\t#\u8fd9\u91cc\u5b58\u5728\u7684\u95ee\u9898\u662f\u65e0\u6cd5\u5206\u5272\u6ca1\u6709\u540e\u7f00\u540d\u7684\u6587\u4ef6\uff0c\u5982\uff1aFileA\n\tglobal fileCount\n\tfileCount = fileCount + 1\n\textPos = sourceFile.rfind('.')\t#str.rfind()\u627e\u5230\u6700\u540e\u4e00\u4e2a\u7b26\u5408\u7684\u5b57\u7b26\u7684index\n\tif extPos &gt; 0:\n\t\treturn sourceFile + '.part' + str(fileCount)\n\telse:\t# extPos == -1\n\t\tprint 'File type? Can not split!'\n\t\tsys.exit(1)\n\ndef combine(filename):\n\tcount = 0\n\textPos = filename.find('.part')\n\tif extPos &gt; 0:\n\t\tfile = filename[:extPos]\n\t\tf1 = open(file, \"wb\")\n\t\twhile True:\n\t\t\tcount = count + 1\n\t\t\tpartFile = file + '.part' + str(count)\n\t\t\tif not exists(partFile):\n\t\t\t\tbreak\n\t\t\telse:\n\t\t\t\tf2 = open(partFile, \"rb\")\n\t\t\t\tcontent = f2.read()\n\t\t\t\tf2.close()\n\t\t\t\tf1.write(content)\n\t\tf1.close()\n\t\tprint 'combine file complete!'\n\n\telse:\n\t\tprint 'File type? Can not combine!'\n\ndef usage():\n\tprint \"nusage is py_fileSplit.py s[c] filename or py_fileSplit.py s[c] filename size(M) n 's' - 'split', 'c' - 'combine\"\n\nif __name__ == \"__main__\":\n\tif len(sys.argv) !=3 and len(sys.argv) !=4:\n\t\tusage()\n\t\tsys.exit(1)\n\tif sys.argv[1] == 's':\n\t\tif len(sys.argv) == 3:\n\t\t\tsplitFile(sys.argv[2])\n\t\telif len(sys.argv) == 4 and int(sys.argv[3]) &gt; 0:\n\t\t\tsplitFile(sys.argv[2], int(sys.argv[3]))\n\t\telse:\n\t\t\tusage()\n\t\tsys.exit(1)\n\n\telif sys.argv[1] =='c':\n\t\tif len(sys.argv) == 3:\n\t\t\tcombine(sys.argv[2])\n\t\telse:\n\t\t\tusage()\n\telse:\n\t\tusage()<\/pre>\n<h6>\u4f7f\u7528\u793a\u4f8b\uff1a<\/h6>\n<p><a href=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2014\/06\/py_fileSplit__usage.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-55\" src=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2014\/06\/py_fileSplit__usage-300x86.jpg\" alt=\"py_fileSplit__usage\" width=\"300\" height=\"86\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7528Python\u5199\u7684\u5927\u6587\u4ef6\u5206\u5272\u811a\u672c\uff1a #!\/usr\/bin\/env python #-*- coding: UT [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,12],"tags":[8],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-programing","category-tools","tag-python"],"views":3178,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=54"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/54\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}