2010-07-01 88 views
3

我想創建一堆目錄/子目錄,我可以將文件複製到。我正在與Python合作,我似乎無法找到一個好辦法來做到這一點。我有一條主要路徑,我將分叉。之後,我擁有Weights和No_Weights。男性和女性以下。在每個男性和女性文件夾中,我有每個種族(高加索人,非洲裔美國人,亞洲人,西班牙人,印度人,其他,未知)。在每個文件夾中,我的年齡範圍從20歲以下,一直到70歲以上(B20,20,30,40,50,60,70)。在python(複雜)創建/製作目錄

我試圖生成所有的路徑,所以我不得不調用mkdir約50次,但這是大約150行代碼(幾乎)。

是否有任何簡單的方法來創建所有這些文件夾,而無需手動完成?

+0

你在做什麼平臺? Python版本? – 2010-07-01 14:56:12

+0

以及我正在使用Python 2.6的Mac上工作(是的,我知道我仍然在使用Python 2.6,只是因爲這個版本和3之間有一個重大變化) – Brandon 2010-07-01 15:20:30

回答

17
import itertools 
import os 

dirs = [["Weights", "No_Weights"], 
     ["Male", "Female"], 
     ["Caucasian", "African-American", "Asian", "Hispanic", "Indo", "Other", "Unknown"], 
     ["B20", "20", "30", "40", "50", "60", "70"]] 

for item in itertools.product(*dirs): 
    os.makedirs(os.path.join(*item)) 

itertools.product()將構建所有可能的路徑變化,然後os.path.join()將加入子路徑一起使用:L所以你不必讓每個子目錄可能是一些使用這裏正確的語法爲您的平臺。

編輯:需要os.makedirs()而不是os.mkdir()。只有前者將構建完整路徑中的所有中間子目錄。

+0

這是一個好主意......我'我從來沒有使用過itertools,但它的作用就像一個魅力,當然它很快!謝謝! – Brandon 2010-07-01 15:21:16

0

有幾個嵌套for循環,然後每個os.mkdir。使用os.path.join將目錄路徑連接在一起。

喜歡的東西:

loop weights 
    mkdir weight 
    loop sexes 
     mkdir weights + sex 
     loop ethnicities 
      mkdir weights + sex + ethnicity 
      loop ages 
       mkdir weights + sex + ethnicity + age 

這裏環路僅僅是一個正常的循環:

for weight in ('weights', 'no_weights'): 

的mkdir是os.mkdir

'+' 是os.path.join:

os.mkdir(os.path.join(weights, sex, ethnicity, age)) 

編輯dir_uti

http://docs.python.org/release/2.5.2/dist/module-distutils.dirutil.html

loop weights 
    loop sexes 
     loop ethnicities 
      loop ages 
       mkpath weights + sex + ethnicity + age 
3

這個例子應該讓你開始:

import itertools 
import os.path 

ROOT = r'C:\base\path' 

sex = ('male', 'female') 
ethnicity = ('Caucasian', 'African-American', 'Asian') 
ages = ('B20', '20', '30') 

for path in itertools.product(sex, ethnicity, ages): 
    print os.path.join(ROOT, *path) 

itertools模塊裏是你的朋友: http://docs.python.org/library/itertools.html#itertools.product

2

就做這樣的事情:

main = 'somedir' 
weight = ['weights', 'No_weights'] 
ethnicity = ['Caucasian', #the rest] 
ages = ['B20'] + range(20, 71, 10) 

for w in weights: 
    os.mkdir(os.path.join(main, w) 
    for e in ethnicity: 
     os.mkdir(os.path.join(main, w, e)) 
     for a in ages: 
      os.mkdir(os.path.join(main, w, e, a)) 

,並應照顧它適合你...

0

os.makedirs可以提供幫助 - 它使所有中間目錄一直到您指定的「葉子」。

另一個問題(產生所有的「一個來自A列,一個來自B列,......」組合)最好接近作爲「混合基地計數」的問題 - 粗略地說,s/thing like。 ..:

choices = [ ['Weights', 'Noweights'], 
      ['Male', 'Female'], 
      ['Caucasian', 'AfricanAmerican', ... 
      ... 
      ] 
Ls = [len(x) for x in choices] 
ct = [0] * len(Ls) 

while True: 
    p = [c[i] for i, c in zip(ct, choices)] 
    os.makedirs(os.path.join(p)) 
    n = -1 
    while n > -len(Ls): 
     ct[n] += 1 
     if ct[n] < Ls[n]: break 
     ct[n] = 0 
     n -= 1 
    else: 
     break 

itertools.product是現代和簡潔的方式來產生所有的「一個從A列,等等等等」選秀權,以及我會建議在製作軟件 - 剛:

for p in itertools.product(*choices): 
    os.makedirs(os.path.join(p)) 

能替換所有上面的代碼(!)。我認爲值得注意的是「混合基地計數」較低抽象級別的方法,因爲它在許多情況下非常方便(包括使用Python版本< 2.6 ;-)的卡住時間。