2012-11-19 71 views
2

我是相當新的編程文件移動到相應的文件夾,這是我在創建使用Python如何搭配和使用Python

我創建該計劃的目的複雜的腳本第一個嘗試是:

  • 要經過文件的列表(360個文件一起在一個文件夾中)
  • 提取3個獨特的字符的文件名和創建基於3個字符的文件夾(60個獨特的文件夾共)
  • 創建for循環經歷源文件夾中的文件列表並將其移至其相應的目標文件夾。

例子:

文件名:KPHI_SDUS81_N3KDIX_201205261956

文件夾中創建基於字符:N3K

import os 

#Creates a list based on file names in the folder 
srcfile=os.listdir("E:\\Learning Python\\Testing out\\thunderstorm stuff") 

#Directiory of where the source files are located 
srcpath= "E:\\Learning Python\\Testing out\\thunderstorm stuff" 

#Creates a list based on the location of where folders will be lcoated. 
#List will be empty since for loop has not ran yet 
targetsrc=os.listdir("E:\\Learning Python\\Testing out\\test folder") 

#path of where the new folders created will be located 
targetpath = "E:\\Learning Python\\Testing out\\test folder" 

#empty list created to hold a string of 3 characters (see for loop below) 
list=[] 

#A list to hold the unique string values. (see 2nd for loop below) 
Target=[] 

#the for loop below looks at a file and gooes to the character place holder index of 12 and looks at the characters UP TO 15. 
#It then assigns the three characters to a variable x which then passes the the string (the 3 characters) to an empty list called list. 
for num in srcfile: 
    x=num[12:15] 
    list.append(x) 

#a test to see if the for loop above was able to exact the three characters from the list 
print list 
print srcfile 

#created to see how big the list is which should match the amount of files in folder 
print len(srcfile) 
print len (list) 

#a function created to make a folder based on a list 
def create(s): 
    targetpath = "E:\\Learning Python\\Testing out\\test folder" 
    test=os.mkdir(os.path.join(targetpath,s)) 

#a dummy variable holder for the for loop below 
valhold = "null" 

#a nested if statement inside a for loop. 
#The for loop goes through all the string values in a list called "list" (assigned to folder in for loop) 
#and checks it against a list called valhold. If folder and valhold are not equal, 
#the values in folder are appened to a list called Target.append which holds unique values. 
#The next step is to create a folder a folder based off the list value "valhold" 
for folder in list: 
    if folder != valhold: 
     Target.append(folder) 
     valhold=folder 
     create(valhold) 
    else: 
     valhold=folder 

#a nested for loop which goes through all the files in the folder for the list "sourcefile" 
#and finds a matching filename 
for dst in Target: 
    wheretonumber=0 
    whereto = targetsrc(wheretonumber) #Name of folder for a given index value "targetsrc" 
    for file in list: 
     filenumber=0 
     filename=srcfile(filenumber) #Name of file for a given index value "sourcefile" 
     if file == dst: 
      ##os.rename(filename(filenumber),whereto(wheretonumber)) 
      ##shutil.move(filename,whereto) 
     filenumber= filenumber+1 
wheretonumber=wheretonumber+1 

我能夠做的第一兩件事在我的子彈上面列出了一個點列表,但很難讓第三個人工作。我研究過shutil.move,os.path.walk和os.rename函數,並沒有運氣讓他們工作。我不斷收到錯誤:

向其中= targetsrc(wheretonumber) 類型錯誤:「名單」對象不是可調用

我有os.rename和shutil.move註釋掉了,因爲我想不同的功能。我的邏輯是正確的方法還是我錯過了什麼?對其他函數有任何建議來嘗試或更改我的代碼以使它將文件移動到文件夾中?

+0

當您更改該行'向其中= targetsrc [wheretonumber]'會發生什麼? – Tim

+0

我也試過,但我收到一個錯誤,指出列表索引超出範圍。 – Sethdd

回答

3

要從list變量中刪除重複項,只需使用內置的set()。並且不要使用list作爲變量名稱,這個陰影內置了list()

列表索引與括號[]不parens。

我看不到你在哪裏分配空白列表(你自己寫的:#List will be empty since for loop has not ran yet)到targetsrc。空列表沒有元素,所以即使L [0]也會超出範圍。

嘗試是這樣的:

import os 
import shutil 

srcpath = "E:\\Learning Python\\Testing out\\thunderstorm stuff" 
srcfiles = os.listdir(srcpath) 

destpath = "E:\\Learning Python\\Testing out\\test folder" 

# extract the three letters from filenames and filter out duplicates 
destdirs = list(set([filename[12:15] for filename in srcfiles])) 


def create(dirname, destpath): 
    full_path = os.path.join(destpath, dirname) 
    os.mkdir(full_path) 
    return full_path 

def move(filename, dirpath): 
    shutil.move(os.path.join(srcpath, filename) 
       ,dirpath) 

# create destination directories and store their names along with full paths 
targets = [ 
    (folder, create(folder, destpath)) for folder in destdirs 
] 

for dirname, full_path in targets: 
    for filename in srcfile: 
     if dirname == filename[12:15]: 
      move(filename, full_path) 
+0

感謝cji讓我的代碼更加簡化了一些。對不起,花了這麼久評論回來;它相當繁忙的一週,我相信你知道。代碼中有一些錯誤,但是我能夠弄清楚它們,修正它們併成功運行它。我在上面的代碼中進行了編輯,以反映我在最後的更正。我感謝您的幫助! – Sethdd