python读取文件夹下的.csv文件

2020-05-13  本文已影响0人  demoxjl
import os
import csv
def readFile(pathFile):
      fileList = os.listdir(pathFile)
      for file in fileList:
            #路径转换为正常路径
            domain = os.path.abspath(pathFile)
             path = os.path.join(domain, file)
              with open(path, 'r', encoding='utf-8') as f:
                        readData = csv.reader(f)
                        result = list(readData)
                        for row in result:
                             print(row)

if __name__ == '__main__':
        #文件路径单‘\’会报错
        pathFile = 'D:\\user\\ddf\csvfile'
        readFile(pathFile)
上一篇 下一篇

猜你喜欢

热点阅读