python - 在两个目录中搜索 excel 文件并创建路径
问题描述
我最近一周发布了一个类似的问题,关于搜索子目录以查找特定的 excel 文件。但是这一次,我需要在两个目录之一中找到一个特定文件,并根据该文件是位于一个文件夹中还是另一个文件夹中给出路径。
这是我到目前为止的代码。我拥有的工作计算机在 Python 2.7.18 上运行 - 没有错误但是当我将 df 打印为 excel 文件时,输出路径中没有显示任何内容
ExcelFilePath = sys.argv[1]
OutputFilePath = sys.argv[2]
# path of excel directory and use glob glob to get all the DigSym files
for (root, subdirs, files) in os.walk(ExcelFilePath):
for f in files:
if '/**/Score_Green_*' in f and '.xlsx' in f:
ScoreGreen_Files = os.path.join(root, f)
for f in ScoreGreen_Files:
df1 = pd.read_excel(f)
df1.to_excel(OutputFilePath)
解决方案
OutputFilePath
是您要传入的参数。除非您将一个作为命令行参数传入,否则它不会有值。
如果要返回路径,则需要返回的变量是ScoreGreen_Files
. 您也不需要遍历,
ScoreGreen_Files
因为它应该只是您要查找的文件。
ExcelFilePath = sys.argv[1]
OutputFilePath = sys.argv[2]
# path of excel directory and use glob glob to get all the DigSym files
for (root, subdirs, files) in os.walk(ExcelFilePath):
for f in files:
if '/**/Score_Green_*' in f and '.xlsx' in f: # f is a single file
ScoreGreen_File = os.path.join(root, f)
df1 = pd.read_excel(ScoreGreen_File)
df1.to_excel(OutputFilePath)
推荐阅读
- x11 - 如何更改 X 窗口属性
- python - 如何找到 csv 的大小并能够在阅读器对象上进行迭代
- mysql - 通过 SSH 隧道将 phpMyadmin 连接到 LIVE MySQL 数据库
- java - 这个带有黄瓜代码的硒的输出令人困惑
- android - 如何过滤 Kotlin 中的两个列表?
- java - 如何在我的 citrus HTTP 请求中发送图像?
- powershell - 取消执行的powershell脚本后如何取消NSIS安装程序?
- python - Raspberry Pi Python 继续循环移动函数,直到特定命令停止它
- php - Codeigniter 分页将我引导至“about:blank#blocked”
- java - 原因:java.net.URISyntaxException:索引 111 处的查询中存在非法字符:由于 URI 中的 {} 出现错误