繁体   English   中英

让 python 脚本仅从包含文件的文件夹运行

[英]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.

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