使用Python导入文件夹中的文件夹名称列表

时间:2017-11-10 15:13:04

标签: python excel pandas

所以我开始尝试自动化某些东西。我的最终游戏是将包含文件名中的清理的Excel文件中的数据组合在一起,并合并这些名为LOV的文件中的选项卡中的数据。所以基本上它必须进入一个文件夹,文件夹中有文件夹,它有2个文件,一个文件在命名中有单词清理,是一个.xlsx文件。我只需要读取这些文件,并将名为LOV的选项卡中的数据拉入一个大文件中。 ---这就是我的最终目标。我刚刚开始,我不在附近,但现在你知道结束游戏了。

目前我只是在主文件夹中找到一个文件夹名称列表,所以我至少知道它到达那里大声笑。

import os
import glob
import pandas as pd


# assigns directory location to PCC Folder
os.chdir('V:/PCC Clean Up Project 2017/_DCS Data SWAT Project/PCC Files 
Complete Ready to Submit/Brake System Parts')
FolderList = glob.glob('')

print(FolderList)

enter image description here

  • 感谢任何帮助,谢谢你们!

2 个答案:

答案 0 :(得分:0)

<强> EDITED

首先很难理解你的问题。但据我所知,您需要遍历文件夹和子文件夹,您可以使用

执行此操作
for root, dirs, files in os.walk(source): #Give your path in source
    for file in filenames:
        if file.endswith((".xlxs")): # You can check for any file extension
           filename = os.path.join(subdir,file)
           dirname = subdir.split(os.path.sep)[-1]  # gets the directory name
           print(dirname)

答案 1 :(得分:0)

如果您只想要当前目录中的文件夹列表,则可以使用os.path。以下是它的工作原理:

import os
directory = "V:/PCC Clean Up Project 2017/_DCS Data SWAT Project/PCC Files 
Complete Ready to Submit/Brake System Parts"
childDirectories = next(os.walk(directory))[1]

这将为您提供当前目录中所有文件夹的列表。

详细了解os.walk here

然后,您可以使用os.chdir

进入其中一个子目录
os.chdir(childDirectories[i])