0

So I've started down the path again of trying to automate something. My end game is to combine the data within Excel files containing the Clean Up in the file name and combine the data from a tab within these files named LOV. So basically it had to go into a folder with folders which have folders again that have 2 files, one file has the words Clean Up in the naming and is a .xlsx file. Which I need to only read those files and and pull the data from the tab called LOV into one large file. --- So that's my end goal. Which I just started and I am no where near, but now you know the end game.

Currently I'm stuck just getting a list of Folder names in the Master folder so I at least know it's getting there lol.

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

  • Any help is appreciated, thanks guys!
Sam Russo
  • 145
  • 1
  • 3
  • 18

2 Answers2

0

EDITED

Firstly Its hard to understand your question. But from what I understand you need to iterate over folders and subfolders, you can do that with

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)
WiLL_K
  • 575
  • 1
  • 3
  • 22
  • Hey Will, this looks to still be giving me file names. The question I was asking is the folder names. I've added an image since my wording seems to be confusing. – Sam Russo Nov 10 '17 at 15:27
  • @SamRussoPalmer Please see the answer above and confirm if it worked for you. And please accept the answer if it solved your problem – WiLL_K Nov 10 '17 at 15:34
0

If you only want the list of folders in your current directory, you can use os.path. Here is how it works:

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]

This will give you a list of all folders in your current directory.

Read more about os.walk here.

You can then go into one of the child directories by using os.chdir:

os.chdir(childDirectories[i])
mhaseebmlk
  • 212
  • 2
  • 5