简体   繁体   中英

Getting python script to run only from folder with files

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))

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.

Ps I do not know how to put the code as python. I can only see Javascript, CSS, and HTML.

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))

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM