[英]Getting python script to run only from folder with files
我有一個 python 腳本,我想從特定文件夾調用 csv 和 xlsx,而無需指定運行程序所需的每個文件的目錄。
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))
所以,我想在我的腳本頂部有一個代碼調用一個文件夾,然后將每個文件路徑寫入每個代碼變得多余。
ps 我不知道怎么把代碼寫成 python。 我只能看到 Javascript、CSS 和 HTML。
如果我理解得很好:
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))
如果你想這樣做,你可以使用“pkg_resources”模塊。 您的數據文件需要在您的包中。
import pkg_resources
pkg_resources.resource_filename('packageName', 'path/of/file/in/my/package.xlsx')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.