一个目录中所有excel文件的异常python脚本



我想知道是否可以通过python脚本访问目录中的所有文件。

脚本如下:

import pandas as pd
import numpy as np
#[4] main_folder=r'C:/Users/MainUser/Desktop/sediment_processing/raw_data/cohesive/'
#[6] section_name = 'UHS_TIG01_PAC07032019'
#[7] file_extension = '.xlsm'
file = main_folder + section_name + file_extension
excel = pd.read_excel(file,sheet_name='ASCII Data',header=3)
df = excel.iloc[1:,21:53]
#And more lines for calculations

我想知道的是,如果有可能,在这个代码中,写一些东西,而不是第[4]、[6]和[7]行,可以将这个代码中的所有计算应用到我目录中的所有excel文件:

C:/Users/MainUser/Desktop/deviction_producing/raw_data/contensive/。

您可以执行以下操作:

import os
folder = '/your/folder/location/'
files = [os.path.join(folder, item) for item in os.listdir(folder)]

这会给你一个文件夹中所有文件的列表,然后你可以对它们做任何你想做的事情。

最新更新