需要帮助使用 OS.WALK 将 for 循环的输出输入 Excel/CSV

Need help getting the output of a for loop into Excel/CSV using OS.WALK

Need help with getting the printed output to a CSV/Excel file.

感谢您的帮助!

import pandas as pd
import os


for root, dirs, files in os.walk("C:/"):
    for file in files:
        if file.endswith(".xlsx"):
             print(os.path.join(root, file))

#Need to print the results of this search into an excel/csv File.

一种可能是将这些行添加到列表中,然后从中创建数据框并将其写入文件。不确定这是否是您要找的。

import pandas as pd
import os

l = []

for root, dirs, files in os.walk(r"C:\"):
    for file in files:
        if file.endswith(".xlsx"):
             l.append(os.path.join(root, file))

pd.DataFrame(l).to_excel(r"C:\Users\______\Desktop\filelist.xlsx", index=False)

如果您需要 CSV 文件,请改用 pandas.to_csv(...)

这个怎么样:

我们可以递归地在您的目录中搜索所有 excel 并将结果打印到数据框,

from pathlib import Path
import pandas as pd

excels = [f for f in Path(r"C:/").rglob('*.xlsx')] # might take a while.
# remove r if you're not on windows.


name = [f.stem for f in excels]
absoloute_path = [f.absolute() for f in excels]
parent = [f.parent for f in excels]
home = [f.home() for f in excels]
parents = {k.stem:'-->'.join([str(i) for i in k.parents][::-1]) for k in excels}

df = pd.DataFrame({'name' : name,
            'abs' : absoloute_path,
             'parent' : parent,
             'home' : home})


final = pd.concat(
    [
        df.set_index("name"),
        pd.DataFrame.from_dict(parents, orient="index", columns=["parents"]),
    ],
    axis=1,
)

Final 将是一个包含文件名、绝对路径、父级、主目录和父级结构的数据框 C:/ -- > C:/foo ---> C:/foo/bar

然后您可以使用 final.to_excel

将其导出到 excel