Python way to read the file folders

In practice, some of the more frequent the operation, but there will be little change files, like daily data, questionnaires and so on. If you modify this file every time inevitably read a lot of trouble, you can not get file folder directly fixed it?

Introducing respective first module

import pandas as pd
import numpy as np
import os

Now questionnaire to read a folder bar Note - paste the file directory, the file requires two '\', in front of a filename for the escape character, the output is List, the cycle can take over the folder, the following replace retaining only one folder:
 

path = "E:\\1月\问卷\\" 
file_name = os.listdir(path)

Then you can read the files you, write the following two kinds of reading my favorite way to excel and csv are:

  • excel
data = pd.read_excel(path+file_name[0])
print(len(data))
data.sample(5)
  • csv
f = open(path+file_name[0],encoding='gbk')
data = pd.read_csv(f)
data.sample(5)

This realization of the contents of the folder where the file to read it!

Storage file with the same name can be read (without suffix): length and content may change according to the actual situation, I usually prefer to add it easier to find the corresponding output.

index = file_name[0].index('.')
files =file_name[0][0:index]

 

Published 19 original articles · won praise 1 · views 2166

Guess you like

Origin blog.csdn.net/wella_liu/article/details/104606210