{"id":1262,"date":"2022-07-29T11:21:18","date_gmt":"2022-07-29T03:21:18","guid":{"rendered":"http:\/\/175.24.184.226\/?p=1262"},"modified":"2023-08-10T16:54:31","modified_gmt":"2023-08-10T08:54:31","slug":"python%e8%af%bb%e5%8f%96%e6%96%87%e4%bb%b6%e5%a4%b9%e4%b8%8b%e7%9a%84%e6%89%80%e6%9c%89%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"\/?p=1262","title":{"rendered":"Python\u8bfb\u53d6\u6587\u4ef6\u5939\u4e0b\u7684\u6240\u6709\u6587\u4ef6"},"content":{"rendered":"<p>os.listdir(path)\u662f\u5f97\u5230\u5728path\u8def\u5f84\u4e0b\u6240\u4ee5\u6587\u4ef6\u7684\u540d\u79f0\u5217\u8868\u3002<\/p>\n<pre><code>open(path)\u662f\u6253\u5f00\u67d0\u4e2a\u6587\u4ef6\u3002\n\niter\u662fpython\u7684\u8fed\u4ee3\u5668\u3002    \n\n\u6240\u4ee5\u8bfb\u53d6\u67d0\u6587\u4ef6\u5939\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u5982\u4e0b\uff1a<\/code><\/pre>\n<ol>\n<li>\n<p><code>import os<\/code><\/p>\n<\/li>\n<li>\n<p><code>path = &quot;D:\/Python34\/news&quot; #\u6587\u4ef6\u5939\u76ee\u5f55<\/code><\/p>\n<\/li>\n<li>\n<p><code>files= os.listdir(path) #\u5f97\u5230\u6587\u4ef6\u5939\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u540d\u79f0<\/code><\/p>\n<\/li>\n<li>\n<p><code>s = []<\/code><\/p>\n<\/li>\n<li>\n<p><code>for file in files: #\u904d\u5386\u6587\u4ef6\u5939<\/code><\/p>\n<\/li>\n<li>\n<p><code>if not os.path.isdir(file): #\u5224\u65ad\u662f\u5426\u662f\u6587\u4ef6\u5939\uff0c\u4e0d\u662f\u6587\u4ef6\u5939\u624d\u6253\u5f00<\/code><\/p>\n<\/li>\n<li>\n<p><code>f = open(path+&quot;\/&quot;+file); #\u6253\u5f00\u6587\u4ef6<\/code><\/p>\n<\/li>\n<li>\n<p><code>iter_f = iter(f); #\u521b\u5efa\u8fed\u4ee3\u5668<\/code><\/p>\n<\/li>\n<li>\n<p><code>str = &quot;&quot;<\/code><\/p>\n<\/li>\n<li>\n<p><code>for line in iter_f: #\u904d\u5386\u6587\u4ef6\uff0c\u4e00\u884c\u884c\u904d\u5386\uff0c\u8bfb\u53d6\u6587\u672c<\/code><\/p>\n<\/li>\n<li>\n<p><code>str = str + line<\/code><\/p>\n<\/li>\n<li>\n<p><code>s.append(str) #\u6bcf\u4e2a\u6587\u4ef6\u7684\u6587\u672c\u5b58\u5230list\u4e2d<\/code><\/p>\n<\/li>\n<li>\n<p><code>print(s) #\u6253\u5370\u7ed3\u679c<\/code><\/p>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u628a\u904d\u5386\u6587\u4ef6\u5939\u7684\u64cd\u4f5c\u5b9a\u4e49\u6210\u4e00\u4e2a\u51fd\u6570\uff0c\u5982\u679c\u662f\u6587\u4ef6\u5939\u5c31\u4e0d\u65ad\u8fed\u4ee3\u904d\u5386\u3002\u8fdb\u800c\u8bfb\u53d6\u6587\u4ef6\u5939\u4e0b\u6240\u6709\u7684\u6587\u4ef6\uff08\u5305\u62ec\u6587\u4ef6\u5939\u91cc\u4e2d\u7684\u6587\u4ef6\uff09<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>os.listdir(path)\u662f\u5f97\u5230\u5728path\u8def\u5f84\u4e0b\u6240\u4ee5\u6587\u4ef6\u7684\u540d\u79f0\u5217\u8868\u3002 open(path)\u662f\u6253\u5f00\u67d0\u4e2a\u6587\u4ef6\u3002 iter\u662fpython   \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":[69],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/1262"}],"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=1262"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/1262\/revisions"}],"predecessor-version":[{"id":1263,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/1262\/revisions\/1263"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1262"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}