2013-05-08 106 views
2

我有以下代碼:os.path.isdir不返回true

path = os.path.join(svn_drive, svn_repo_path, relative_path) 
if os.path.isdir(path.encode('string-escape')):    
      print path, " is a directory" 
else:    
      print path, " is not a directory" 

這導致了以下內容:

D:\mysvn\trunk\Assets\myfile.max is not a directory  
D:\mysvn\\Animations is not a directory 
.... 

即問題是os.path.isdir似乎沒有認識到path實際上是一個目錄

svn_drive是這種情況下的驅動器號D: svn_repo_path是在這種情況下mysvn relative path是相對於將svn路徑

我試圖逃脫,不逸出(即我通過解析SVN的日誌的結果獲得),許多os.path方法(abspathbasename,等),似乎沒有任何工作:(

我也接受備選方案;),我只是想能夠知道一個路徑,然後通過電子郵件發送文件,不介意如何(我知道有時候人們想保持他們的代碼,但這只是一個獨立的腳本)

我還需要打開該文件在後一階段,以電子郵件發送,我得到沒有發現,我要猜一個文件從這裏開始

全功能列表(如果它幫助):

def parse_svn_results(lines, svn_drive, svn_repo_path): 
    result = [] 
    for x in lines.split("\n"): 
     if "trunk/" in x: 
      relative_path = x.lstrip('MDA ').replace("/","",1).replace("/", os.sep) 
      path = os.path.join(svn_drive, svn_repo_path, relative_path) 
      if os.path.isdir(path.encode('string-escape')):    
       print path, " is a directory" 
      else:    
       print path, " is not a directory" 
       result.append(path) 
    return result 

UPDATE

這是代碼的一種變通方法版本,但我仍然不能做imghdr.what(filename)(其中filename是結果的文件之一)

def parse_svn_results(lines, svn_drive, svn_repo_path): 
    result = [] 
    for x in lines.split("\n"): 
     if "trunk/" in x: 
      relative_path = x.lstrip('MDA ').replace("/", "", 1).replace("/", os.sep) 
      temp_path = os.path.join(svn_drive, os.sep, svn_repo_path, relative_path) 
      path = format_path(temp_path) 
      if path is not None: 
       result.append(path) 
    return result 


def format_path(file_destination): 
    file_name = os.path.basename(file_destination) 
    path = os.path.dirname(file_destination) 
    base, ext = os.path.splitext(file_name) 
    picture_format = None 
    e = ext if picture_format is None else '.%s' % picture_format.lower() 
    if e: 
     to_path = os.path.join(path, base + e) 
     return to_path 
+1

究竟是什麼'svn_drive',' svn_repo_path'和'relative_path'。他們是如何產生的?你爲什麼使用'.encode()'? – 2013-05-08 09:26:41

+0

更新了與該信息的問題 – roundcrisis 2013-05-08 09:29:01

+0

我使用編碼,因爲我在REPL中嘗試相同的東西,它的工作(即isdir返回true) – roundcrisis 2013-05-08 10:19:12

回答

1

我認爲svn_drive應該是D:/而不是D:。看到這一行的documentation

Note that on Windows, since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.

+0

我剛試過這並沒有什麼區別。我也嘗試在REPL中運行它,並且os.path.join('D:',os.sep,'whatever')是返回有效路徑的唯一東西: >>> os.path.join ('D:','mypath') 'D:mypath' >>> os.path。加入( 'd:\', 'mypath中') 文件 「」,第1個 os.path.join( 'd:\', 'mypath中') ^ 語法錯誤:無效的語法 >>> OS。 path.join( 'd:\\, 'mypath中') 文件 「」,第1個 os.path.join(' d:\\, 'mypath中') ^ 語法錯誤:無效的語法 >>> os.path.join('D:',os.sep,'mypath') 'D:\\ mypath' – roundcrisis 2013-05-08 09:50:54

+0

對不起,我在這臺機器上沒有VM,os.path.join('D: /','不管')工作? – 2013-05-08 09:52:25

+0

不用擔心,我在REPL上試過了,它返回'D:/ mypath',然後我嘗試了上面的腳本和相同的目錄(dirs無法識別):d:/ mysvn \ trunk \ TexturePacker不是目錄 – roundcrisis 2013-05-08 09:56:29

1

我沒有看到一個點在使用encode但如果它是絕對必要的話,我建議你進一步調節它的結果將其通過os.path.normpath,然後把它扔在os.path.isdir

+0

我沒有問題,刪除編碼,當我這樣做時,我得到相同的行爲。有點無關,但即使嘗試imghdr.what(os.path.normpath(文件名))或imghdr.what(文件名)或編碼,他們都給我一個IO錯誤(我已驗證文件在那裏) – roundcrisis 2013-05-08 11:40:36

0

好了,該解決方案從另一個問題就來了,我在年底剝離\ R,我相信這是一個由path.join添加,但沒了 python filename on windows

+0

在類似情況下'打印所傳遞的變量通常在調試時非常有用。請詳細說明答案並將其標記爲已回答。 – 2013-05-08 18:06:15

+0

感謝您的答案順便說一句,我確實使用print,但我愚蠢地(?)認爲os.path.normpath會失敗或處理它看起來不像路徑並修復它的事實。 – roundcrisis 2013-05-08 18:25:51

+1

'help'確實方便快速查找。只需'help(os.path.normpath)'。另外你可以使用'op = os.path'來保存一些輸入。並且請點擊評論框右下角的幫助鏈接以獲得一些快速格式化小技巧。它們使閱讀帖更容易,類似於使閱讀和書寫代碼更容易的縮進。 – 2013-05-08 18:51:02