2013-05-13 103 views
2

我是一個自學成才的Python新手,我已經對python的運行方式有了一個基本的瞭解,但是我堅持以下幾點。在Python中重命名文件

我有一個文本文件列表,它是郵箱名稱的交換服務器郵件轉儲。我有數百個這些文本文件,它們目前的名稱格式爲Priv_date.txt,例如Priv_02JAN2004.txt。我需要能夠告訴他們來自哪些服務器,因此在這些文本文件中,我想要讀取具有實際郵件服務器名稱(服務器:MAILSERVER1)的10行,並將其添加或附加到原始文件名。

我想結束的文件名是MAILSERVER1_PRIV_02JAN2004.txt。我對自己能做什麼也做不了,對文件路徑和名稱感到困惑,但看不出我做錯了什麼。據我得爲這樣:

import os,sys 

folder = "c://EDB_TEMP" 

for root, dirs, filenames in os.walk(folder): 
    for filename in filenames: 
     fullpath=os.path.join(root,filename) 
     filename_split = os.path.splitext(fullpath) 

     #print fullpath 
     #print filename 

     with open (fullpath, "r") as tempfile: 
      for line in tempfile.readlines(): 
       if "Server:" in line: 
        os.rename(tempfile,+line[10:]+fullpath) 

但我不斷收到此錯誤:

error is TypeError: bad operand type for unary +: 'str'

回答

2

此代碼的工作,並做你的描述

#Also include Regular Expression module, re 
import os,sys,re 

#Set root to the folder you want to check 
folder = "%PATH_TO_YOUR_FOLDER%" 

#Walk through the folder checking all files 
for root, dirs, filenames in os.walk(folder): 
    #For each file in the folder 
    for filename in filenames: 
     #Create blank strink for servername 
     servername = '' 
     #Get the full path to the file 
     fullpath=os.path.join(root,filename) 
     #Open the file as read only in tempfile 
     with open (fullpath, "r") as tempfile: 
      #Iterate through the lines in the file 
      for line in tempfile.readlines(): 
       #Check if this line contains "Server: XXXXX" 
       serverline= re.findall("Server: [a-zA-Z0-9]+", line) 
       #If the line was found 
       if serverline: 
        #Split the line around ": " and take second part as server name 
        sname = serverline[0].split(": ") 
        #Set servername variable so isn't lost outside scope of with block 
        servername = sname[1] 
     #If a servername was found for that text file 
     if len(servername) > 0: 
      #Rename the file 
      os.rename(fullpath,root+'\\'+servername+filename) 

這樣做是走的目錄,如你收到,發現每個路徑。對於每個文件,它將獲得路徑,打開文件並查找包含Server:SERVERNAME的行。然後它將提取SERVERNAME並將其放入servername變量中。文件完成後,它將被關閉,腳本將檢查該文件是否生成了服務器名字符串。如果是這樣,它通過用SERVERNAME加前綴來重命名文件。

我有一段時間所以決定測試它,所以應該做你想做的事

+0

感謝您的輸入。我可以看到我在原文中錯過了什麼。我一直在看你已經添加的代碼o0rebelious0o(謝謝!)我已經通過了它,所以它對我來說很有意義,但我仍然得到一個錯誤:[Error 32]進程無法訪問文件,因爲它是被另一個進程使用 不知道爲什麼。我已經使用過Proc Explorer,並檢查了開始解鎖,我甚至嘗試添加一個新的路徑到根變量,所以重命名應該發生在不同的目錄中,但似乎沒有任何區別。我正在使用Windows 7x64位操作系統。 – user2377057 2013-05-13 19:54:11

+0

它給你這個錯誤的原因是因爲你試圖在while文件打開塊內重命名文件。文件處於打開狀態時,操作系統會鎖定它,因此無法重命名。使用python時,需要注意的一點是,縮進塊的處理方式與使用java和C等語言中的加緊代碼相同,所有縮進的代碼都在相同條件下執行,即在with語句中執行。你所需要做的就是把if()語句與with open語句聯繫起來,它應該在理論上起作用。另請檢查創建.txt日誌的進程是否未運行 – o0rebelious0o 2013-05-13 21:46:48

+0

再次感謝。這就說得通了。我可能要求在一根棍子上的月球......在Python中:-) ..但我現在得到一個錯誤123文件名,目錄,卷是不正確的。我已經完成了最後一行的打印,並且它(或者也許是我)似乎將一個eol字符放在行中,所以輸出看起來像MAILSERVER \ nPRIV_02JAN2004.txt。我看不出爲什麼。 – user2377057 2013-05-14 05:29:33

3

您的os.rename(tempfile,+line[10:]+fullpath)錯誤逗號似乎放錯了地方。

該錯誤基本上說,+只是在逗號不能在字符串之前,即行[10:]。

+0

這個。 OP:你應該使用一個編輯器來指出你這樣的明顯錯誤,比如Vim + Synstastic,Gedit +插件甚至IDE。它會爲你節省很多時間。 – timss 2013-05-13 13:04:27