-2
for v in enumerate (["20170106", "20170113", "20170127", "20170203", "20170210", "20170224", "20170303", "20170310", "20170324", "20170331"])
    Year17 = "/Users/Name/Desktop/Datas/" + v
    csvFiles = glob.glob (os.path.join (Year17, "*.csv"))
    df = (pd.read_csv(i) for i in csvFiles)
    df = pd.concat (df, ignore_index=True)

The subfolders "20170106", "20170113", "20170127", etc... which are inside the folder "Year17"

This code works good, but:

  • How can I avoid writing all the subfolders names "20170106", "20170113", "20170127", etc... in the for sentence? Maybe os.walk? How it would be?
Dan_99
  • 49
  • 5
  • 6
    Possible duplicate of [Getting a list of all subdirectories in the current directory](https://stackoverflow.com/questions/973473/getting-a-list-of-all-subdirectories-in-the-current-directory) – Vikash Singh Jan 28 '18 at 11:58

1 Answers1

0

You could use the pathlib library:

from pathlib import Path

base_path = Path("/Users/Name/Desktop/Datas/")
for v in enumerate(base_path.iterdir()):
    print(v)

If you only want the sub directories and not the files, add an if statement:

    if v[1].is_dir():
        print(v)
MegaIng
  • 7,361
  • 1
  • 22
  • 35