Python 讀文件方法

讀目錄文件名:python

import os
import io
def eachFile(filepath):
    pathDir = os.listdir(filepath)
    for allDir in pathDir:
        child = os.path.join('%s%s' % (filepath, allDir))
        print child.decode('gbk')  # .decode('gbk')是解決中文顯示亂碼問題

打印文件內容:code

# 讀取文件內容並打印
 def readFile(filename):
     fopen = open(filename, 'r')  # r 表明read
     for eachLine in fopen:
         print "讀取到得內容以下:", eachLine
     fopen.close()
def files:
    fopen = io.open(filepath, 'r', encoding='gbk')  # r 表明read

    while True:
        fopen1 = fopen.readline()
        if fopen1:
            print fopen1
        else:
            break
    fopen.close()
相關文章
相關標籤/搜索