I have a python script which I would like to call csv and xlsx from a specific folder without having to specify the directory of each files needed to run the program.
import pandas as pd
df = pd.read_excel("WindDirectionWest2.xlsx")
df.drop(df.columns[df.columns.str.contains('unnamed',case = False)],axis = 1, inplace = True)
hey=df["WindDirection"].mean()
print(int(hey))
df = pd.read_excel("WindDirectionSouth2.xlsx")
df.drop(df.columns[df.columns.str.contains('unnamed',case = False)],axis = 1, inplace = True)
he=df["WindDirection"].mean()
print(int(he))
df = pd.read_excel("WindDirectionEast2.xlsx")
df.drop(df.columns[df.columns.str.contains('unnamed',case = False)],axis = 1, inplace = True)
he=df["WindDirection"].mean()
print(int(he))
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
So, I would like to have a code in top of my script calling a folder which then makes it redundant to write each file path to each code.
P.s I do not know how to put the code as python. I can only see Javascript, CSS, and HTML.
CodePudding user response:
If I understand well:
import os
import pandas as pd
path = '/some/path/to/file'
for file in os.listdir(path):
df = pd.read_excel(os.path.join(path, file))
CodePudding user response:
If you want to do so, you can use "pkg_resources" module! Your data file needs to be inside your package.
import pkg_resources
pkg_resources.resource_filename('packageName', 'path/of/file/in/my/package.xlsx')