I have a question from the following discussion:
How to find the position/index of a particular file in a directory?
Suppose I have three excel files in a folder: test_3d, test_3d1, test_3d2
It says we can read the index of a file from the following codes
folder = r'C:\Users\Denny\Desktop\Work\test_read'
files = os.listdir(folder)
files.index('test_3d1.xlsx')
>> 1
Also, we can read the data of each file by
folder = r'C:\Users\Denny\Desktop\Work\test_read'
files = os.listdir(folder)
dfs = {}
for file in files:
if file.endswith('.xlsx'):
dfs[file[:-5]] = pd.read_excel(os.path.join(folder,file), header = None, skiprows=[0], usecols = "B:M")
dfs['test_3d1']
Also, we can show all its files by using
files
>> ['test_3d.xlsx', 'test_3d1.xlsx', 'test_3d2.xlsx']
My question now is how to get the data of each file not by its name
dfs['test_3d1']
but by its index, for example
dfs['files[1]'] # I want to pick up the 2nd file 'text_3d1' from files.
However, it shows an error
How to fix this error?