{"id":382,"date":"2018-07-01T13:03:51","date_gmt":"2018-07-01T13:03:51","guid":{"rendered":"http:\/\/jianghengqiang.info\/wp\/?p=382"},"modified":"2018-07-01T13:03:51","modified_gmt":"2018-07-01T13:03:51","slug":"python%e7%bc%96%e7%a8%8b%e8%87%aa%e5%8a%a8%e5%8c%96%e5%bf%ab%e9%80%9f%e4%b8%8a%e6%89%8b%e9%83%a8%e5%88%86%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/jianghengqiang.info\/wp\/?p=382","title":{"rendered":"Python\u7f16\u7a0b\u81ea\u52a8\u5316\u5feb\u901f\u4e0a\u624b\u90e8\u5206\u7a0b\u5e8f"},"content":{"rendered":"<p>\u7b2c9\u7ae0\u7ec4\u7ec7\u6587\u4ef6<\/p>\n<p>9.4 \u9879\u76ee\uff1a\u5c06\u5e26\u6709\u7f8e\u56fd\u98ce\u683c\u65e5\u671f\u7684\u6587\u4ef6\u6539\u540d\u4e3a\u6b27\u6d32\u98ce\u683c\u65e5\u671f<\/p>\n<p>168\u9875 \u7b2c4\u6b65\uff1a\u7c7b\u4f3c\u7a0b\u5e8f\u7684\u60f3\u6cd5<\/p>\n<p>\u4e3a\u6587\u4ef6\u540d\u6dfb\u52a0\u524d\u7f00\uff0c\u8bf8\u5982\u6dfb\u52a0spam_\u3002\u5c06eggs.txt\u6539\u540d\u4e3aspam_eggs.txt.<\/p>\n<p>import shutil,os<br \/>\nfolder=&#8217;F:\\\\pythonExercise\\\\Project95Test&#8217;<br \/>\nnewfolder=&#8217;F:\\\\pythonExercise\\\\Project93Test1&#8242;<\/p>\n<p>for foldername,subfolders,oldfilenames in os.walk(folder):<br \/>\nabsWorkingDir=os.path.abspath(foldername)<br \/>\nfor oldfilename in oldfilenames:<br \/>\nnewfilename=&#8217;spam_&#8217;+oldfilename<\/p>\n<p>oldAbsName=os.path.join(absWorkingDir,oldfilename)<br \/>\nnewAbsName=os.path.join(absWorkingDir,newfilename)<br \/>\nprint(&#8216;Renaming &#8220;%s&#8221; to &#8220;%s&#8221; &#8230;&#8217; %(oldfilename,newfilename))<br \/>\n#Rename the files.<br \/>\nshutil.move(oldAbsName,newAbsName)<\/p>\n<p>\u5220\u9664\u6587\u4ef6\u540d\u4e2d\u76840,\u8bf8\u5982spam0042.txt.<\/p>\n<p>#! python3<br \/>\n# \u53bb\u9664\u6587\u4ef6\u540d\u4e2d\u76840\uff0c\u4f8b\u5982\u5c06spam0042.txt\u91cd\u547d\u540d\u4e3aspam42.txt.<br \/>\nimport shutil,os,re<br \/>\nfolder=&#8217;F:\\\\pythonExercise\\\\Project933Test&#8217;<br \/>\nnameRegex=re.compile(&#8216;0&#8217;)<br \/>\nfor foldername,subfolders,oldfilenames in os.walk(folder):<br \/>\nabsWorkingdir=os.path.abspath(foldername)<br \/>\nfor oldfilename in oldfilenames:<br \/>\nnewFilename=nameRegex.sub(&#8221;,oldfilename)<br \/>\noldWholename=os.path.join(absWorkingdir,oldfilename)<br \/>\nnewWholename=os.path.join(absWorkingdir,newFilename)<\/p>\n<p>#Rename the files.<br \/>\nprint(&#8216;Renaming &#8220;%s&#8221; to &#8220;%s&#8221; &#8230;&#8217; %(oldfilename,newFilename))<br \/>\nshutil.move(oldWholename,newWholename)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b2c9\u7ae0\u7ec4\u7ec7\u6587\u4ef6 9.4 \u9879\u76ee\uff1a\u5c06\u5e26\u6709\u7f8e\u56fd\u98ce\u683c\u65e5\u671f\u7684\u6587\u4ef6\u6539\u540d\u4e3a\u6b27\u6d32\u98ce\u683c\u65e5\u671f 168 &hellip; <a href=\"https:\/\/jianghengqiang.info\/wp\/?p=382\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-382","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/jianghengqiang.info\/wp\/index.php?rest_route=\/wp\/v2\/posts\/382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianghengqiang.info\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianghengqiang.info\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianghengqiang.info\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jianghengqiang.info\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=382"}],"version-history":[{"count":1,"href":"https:\/\/jianghengqiang.info\/wp\/index.php?rest_route=\/wp\/v2\/posts\/382\/revisions"}],"predecessor-version":[{"id":383,"href":"https:\/\/jianghengqiang.info\/wp\/index.php?rest_route=\/wp\/v2\/posts\/382\/revisions\/383"}],"wp:attachment":[{"href":"https:\/\/jianghengqiang.info\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianghengqiang.info\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianghengqiang.info\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}