python读取指定路径下的所有文件----比赛之后(备忘录)

之前的比赛要求读取指定路径下的文本文件,打印其信息。

如果按下回车就继续下一个,如果按下esc就退出。

getFiles可以获取指定路径下的所有CSV文件。可以自己修改,加上递归更可以深度遍历所给路径下的包括子路径下的文件。

获取文件类型也可以自己修改。

再提一点,这段程序在python.exe运行很正常  但是在pycharm由于编译器的问题导致esc不管用。。。

# --coding:utf-8-- #

import msvcrt
import sys
import os
reload(sys)
sys.setdefaultencoding('utf-8')

environment_code_type=sys.getfilesystemencoding()


raw_input()
def getFiles(path,resultFiles):
    if os.path.exists(path):
        for root,dirs,files in os.walk(path):
            for file in files:
                if str(file).endswith('.csv'):
                    resultFiles.append(os.path.join(root,file))
Files=[]
getFiles('E:/ftpfiles',Files)
for f in Files:
    ff = open(f)
    print(ff.read())
    print('打开文件成功\n如果继续读取下一个文件请按下enter\n如果想退出请按下esc.'.decode('utf-8').encode(environment_code_type))
    a = msvcrt.getch()
    if(a==''):
        continue
    if ord(a)==27:
        break

猜你喜欢

转载自blog.csdn.net/u010014073/article/details/79942254