2014-03-12 69 views
0

由於我對Python非常陌生,我寫了一些我認爲是一個非常簡單的腳本來查找丟失的目錄,我提前道歉。該腳本一次從文件讀入一行(每行包含一個unix樣式目錄),然後檢查該目錄是否存在,如果該目錄不存在,則會顯示一條消息,並將該目錄寫入輸出文件。python os.path.exists not working

的問題是,我在輸出文件存在的目錄,我不知道,爲什麼這是...

我肯定知道在輸出文件中列出的第一個目錄中,我已經從文件的開頭檢查了大約十幾個。我一直看到第一個目錄確實存在,但其他我沒有檢查過。我甚至在python交互模式下運行了相同的檢查,並且它返回True,所以我不明白爲什麼將這個目錄寫入輸出文件。

有什麼想法?下面的輸入文件的

import os 
f = open('missingdirs.out', 'w') 
for line in file('alldirs.txt', 'r'): 
    if not os.path.exists(line.strip()): 
      print "Could not find the path specified: " + line.strip() 
      f.write(line.strip()+'\n') 
f.close() 

樣品(這些是絕對路徑):

/home/sites/shared/lingui/course/0418-0001_AUT_Can 
/home/sites/shared/lingui/course/0418-0001_AUT_Do 
/home/sites/shared/lingui/course/0418-0001_AUT_How 
/home/sites/shared/lingui/course/0418-0001_AUT_Is-Are 
/home/sites/shared/lingui/course/0418-0001_AUT_What 
/home/sites/shared/lingui/course/0418-0001_AUT_When 
/home/sites/shared/lingui/course/0418-0001_AUT_Where 
+2

您還可以添加輸入文件的樣本嗎? – Dyrborg

+0

聽起來像'alldirs.txt'不像你認爲的那樣工作 –

+0

此外 - 當你使用.strip()時,你只刪除空格 - 確保你沒有\ n,\ r或類似的東西在你的字符串,以及當你檢查目錄是否存在。 – Dyrborg

回答

0

一些簡單的故障排除步驟....

with open('alldirs.txt','r') as in_: 
    lines = list(map(str.strip,in_.readlines())) 
print('\n'.join(lines[:10])) 
# maybe your input is not what you think it is 

import os 
path = r"/home/sites/shared/lingui/course/0418-0001_AUT_Can" 
print(os.path.exists(path)) 
# use a string literal rather than the file read -- does THAT work? 

我可以保證所有,但該問題與您的輸入文件格式不正確。

+0

非常感謝你,我會試試這個並回復你。我現在只是陷入了一些其他工作。 – Mallocai