{"id":1301,"date":"2022-09-09T16:18:59","date_gmt":"2022-09-09T08:18:59","guid":{"rendered":"http:\/\/175.24.184.226\/?p=1301"},"modified":"2022-09-09T16:18:59","modified_gmt":"2022-09-09T08:18:59","slug":"python%e5%a6%82%e4%bd%95%e5%af%b9%e6%96%87%e4%bb%b6%e8%bf%9b%e8%a1%8c%e9%87%8d%e5%91%bd%e5%90%8d","status":"publish","type":"post","link":"\/?p=1301","title":{"rendered":"Python\u5982\u4f55\u5bf9\u6587\u4ef6\u8fdb\u884c\u91cd\u547d\u540d"},"content":{"rendered":"<h2>\u5bf9\u6587\u4ef6\u8fdb\u884c\u91cd\u547d\u540d<\/h2>\n<p>\u76f4\u63a5\u8d34\u4ee3\u7801\uff1a\u00a0<\/p>\n<pre><code>import os<\/code><\/pre>\n<pre><code>srcFile = &#039;.\/actwork\/linkFile\/allExtLinks - \u526f\u672c.txt&#039;\ndstFile = &#039;.\/actwork\/linkFile\/allExtLinks - copy.txt&#039;\ntry:\n\u00a0 \u00a0 os.rename(srcFile,dstFile)\nexcept Exception as e:\n\u00a0 \u00a0 print(e)\n\u00a0 \u00a0 print(&#039;rename file fail\\\\r\\\\n&#039;)\nelse:\n\u00a0 \u00a0 print(&#039;rename file success\\\\r\\\\n&#039;)<\/code><\/pre>\n<p><a href=\"https:\/\/www.jb51.net\/article\/251067.htm\"><\/a><\/p>\n<h2>\u5bf9\u6587\u4ef6\u5939\u4e0b\u6587\u4ef6\u6309\u987a\u5e8f\u91cd\u547d\u540d<\/h2>\n<p>\u5728\u5904\u7406\u673a\u5668\u5b66\u4e60\u7684\u56fe\u7247\u65f6\uff0c\u9700\u8981\u5c06\u56fe\u7247\u6839\u636e\u7c7b\u522b\u653e\u5165\u4e0d\u540c\u6587\u4ef6\u5939\u4e2d\uff0c\u5728\u6587\u4ef6\u5939\u4e0b\u4e5f\u60f3\u5c06\u56fe\u7247\u7528\u6570\u5b57\u9012\u589e\u91cd\u547d\u540d\u4e00\u4e0b\uff0c\u65b9\u4fbf\u540e\u7eed\u5904\u7406\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u770b\u4e00\u4e0bpython\u7684\u91cd\u547d\u540d\u51fd\u6570\uff1a<\/p>\n<pre><code>os.rename(src,dst)<\/code><\/pre>\n<ul>\n<li><code>src<\/code>:\u8981\u4fee\u6539\u7684\u76ee\u5f55\u540d<\/li>\n<li><code>dst<\/code>:\u4fee\u6539\u540e\u7684\u76ee\u5f55\u540d<\/li>\n<\/ul>\n<p>\u5982\u679cdst\u662f\u4e00\u4e2a\u5b58\u5728\u7684\u76ee\u5f55, \u5c06\u629b\u51faOSError\u3002<\/p>\n<p>\u6ce8\u610f\uff1asrc\u548cdst\u90fd\u662f\u5b8c\u6574\u8def\u5f84+\u6587\u4ef6\u540d<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u5148\u5bf9\u6587\u4ef6\u5939\u91cd\u547d\u540d<\/p>\n<p><a href=\"https:\/\/www.jb51.net\/article\/251067.htm\"><\/a><\/p>\n<h3>\u5148\u4f7f\u7528\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5c1d\u8bd5\u4e00\u4e0b<\/h3>\n<pre><code>import os\ndef myrename(path):\n\u00a0 \u00a0 file\\_list=os.listdir(path)\n\u00a0 \u00a0 i=0\n\u00a0 \u00a0 for fi in file\\_list:\n\u00a0 \u00a0 \u00a0 \u00a0 old\\_name=os.path.join(path,fi)\n\u00a0 \u00a0 \u00a0 \u00a0 new\\_name=os.path.join(path,str(i))\n\u00a0 \u00a0 \u00a0 \u00a0 os.rename(old\\_name,new\\_name)\n\u00a0 \u00a0 \u00a0 \u00a0 i+=1\nif \\_\\_name\\_\\_==&quot;\\_\\_main\\_\\_&quot;:\n\u00a0 \u00a0 path=&quot;D:\/test\/121&quot;\n\u00a0 \u00a0 myrename(path)<\/code><\/pre>\n<p><img src=\"https:\/\/img.jbzj.com\/file_images\/article\/202206\/2022061010161511.png\" alt=\"\" \/><\/p>\n<p><a href=\"https:\/\/www.jb51.net\/article\/251067.htm\"><\/a><\/p>\n<h3>\u6211\u4eec\u5c31\u53ef\u4ee5\u770b\u5230\u6587\u4ef6\u5939\u5df2\u7ecf\u6309\u7167\u987a\u5e8f\u91cd\u547d\u540d\u4e86<\/h3>\n<p>\u7a0d\u5fae\u6539\u52a8\u4e00\u4e0b\uff0c\u663e\u5f97\u6ca1\u6709\u90a3\u4e48low<\/p>\n<pre><code>import os\ndef myrename(path):\n    file\\_list=os.listdir(path)\n    for i,fi in enumerate(file\\_list):\n        old\\_name=os.path.join(path,fi)\n        new\\_name=os.path.join(path,&quot;N0.&quot;+str(i))\n        os.rename(old\\_name,new\\_name)\nif \\_\\_name\\_\\_==&quot;\\_\\_main\\_\\_&quot;:\n    path=&quot;D:\/test\/121&quot;\n    myrename(path)<\/code><\/pre>\n<p><img src=\"https:\/\/img.jbzj.com\/file_images\/article\/202206\/2022061010161512.png\" alt=\"\" \/><\/p>\n<p>\u6548\u679c\u662f\u4e00\u6837\u7684,enumerate\u51fd\u6570\u7684\u4f5c\u7528\u5c31\u4e0d\u518d\u8d58\u8ff0\u4e86\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528python zip\u6765\u8fdb\u884c\u4fee\u6539\uff1a<\/p>\n<pre><code>import os\ndef myrename(path):\n    file\\_list=os.listdir(path)\n    for i,fi in zip(range(len(file\\_list)),file\\_list):\n        old\\_name=os.path.join(path,fi)\n        new\\_name=os.path.join(path,&quot;The.&quot;+str(i))\n        os.rename(old\\_name,new\\_name)\nif \\_\\_name\\_\\_==&quot;\\_\\_main\\_\\_&quot;:\n    path=&quot;D:\/test\/121&quot;\n    myrename(path)<\/code><\/pre>\n<p>\u6548\u679c\u4e00\u6837\uff1a<\/p>\n<p><img src=\"https:\/\/img.jbzj.com\/file_images\/article\/202206\/2022061010161513.png\" alt=\"\" \/><\/p>\n<p><a href=\"https:\/\/www.jb51.net\/article\/251067.htm\"><\/a><\/p>\n<h3>\u63a5\u4e0b\u6765\u6211\u4eec\u5bf9\u6587\u4ef6\u8fdb\u884c\u91cd\u547d\u540d<\/h3>\n<p>\u4e0a\u9762\u7684\u4e09\u79cd\u65b9\u6cd5\u90fd\u662f\u53ef\u4ee5\u4f7f\u7528\u7684\uff0c\u53ea\u9700\u8981\u7a0d\u5fae\u6539\u52a8\u4e00\u4e0b,\u6211\u4ee5\u51fd\u6570enumerate\u4e3a\u4f8b\u6f14\u793a\u4e00\u4e0b\uff1a<\/p>\n<p><img src=\"https:\/\/img.jbzj.com\/file_images\/article\/202206\/2022061010161514.png\" alt=\"\" \/><\/p>\n<p><a href=\"https:\/\/www.jb51.net\/article\/251067.htm\"><\/a><\/p>\n<h3>\u5148\u5efa\u4e00\u4e2a\u6587\u672c\u6587\u6863<\/h3>\n<p>\u590d\u5236\u7c98\u8d34\u4e00\u5806\uff0c\u4f5c\u4e3a\u6d4b\u8bd5\u7528\uff0c\u5728\u51fd\u6570\u4e2d\u52a0\u5165\u4e00\u4e2atry-except\uff0c\u9632\u6b62\u6587\u4ef6\u91cd\u547d\u540d\u51fa\u9519\uff0c\u8fd9\u91cc\u91cd\u547d\u540d\u9700\u8981\u662f\u76f8\u540c\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u5426\u5219\u5c31\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre><code>import os\ndef myrename(path):\n    file\\_list=os.listdir(path)\n    for i,fi in enumerate(file\\_list):\n        old\\_dir=os.path.join(path,fi)\n        filename=&quot;my&quot;+str(i+1)+&quot;.&quot;+str(fi.split(&quot;.&quot;)\\[-1\\])\n        new\\_dir=os.path.join(path,filename)\n        try:\n            os.rename(old\\_dir,new\\_dir)\n        except Exception as e:\n            print(e)\n            print(&quot;Failed!&quot;)\n        else:\n            print(&quot;SUcess!&quot;)\nif \\_\\_name\\_\\_==&quot;\\_\\_main\\_\\_&quot;:\n    path=&quot;D:\/test\/121&quot;\n    myrename(path)<\/code><\/pre>\n<p><img src=\"https:\/\/img.jbzj.com\/file_images\/article\/202206\/2022061010161515.png\" alt=\"\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u6587\u4ef6\u8fdb\u884c\u91cd\u547d\u540d \u76f4\u63a5\u8d34\u4ee3\u7801\uff1a\u00a0 import os srcFile = &#039;.\/actwork\/linkFile\/allExt   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[53,69],"tags":[16],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/1301"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1301"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/1301\/revisions"}],"predecessor-version":[{"id":1302,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/1301\/revisions\/1302"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1301"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}