我想对与我的脚本位于同一文件夹中且符合命名约定的所有文件执行一些数据清理。数据清理我很好,但它只是我正在努力解决的同一个文件夹。以前的工作代码:import glob
for filename in glob.glob('C:/Users/<me>/Downloads/New Folder/Forecast and Inventory *.xlsx'):当前代码:import os
for filename in os.getcwd() + '\Forecast and Inventory *.xlsx':我得到错误代码No such file or directory: 'C'我是否需要进行某种替换才能将 \s 转换为 /s?正如代码所示,我想要所有以“预测和库存”开头的文件。谢谢!
1 回答

幕布斯7119047
TA贡献1794条经验 获得超8个赞
您正在遍历一个字符串。
因为os.getcwd() + '\Forecast and Inventory *.xlsx'是字符串"C://**working-dir/Forecast and Inventory *.xlsx"
所以第一次迭代filename是字符串'C'。
例如
for filename in os.getcwd() + '\Forecast and Inventory *.xlsx':
print(filename)
将返回
"C"
":"
"/"
...
你只需要把路径放在里面glob.glob
for filename in glob.glob(os.getcwd() + '\Forecast and Inventory *.xlsx'):
# do something
添加回答
举报
0/150
提交
取消