我有以下代碼: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
方法(abspath
,basename
,等),似乎沒有任何工作:(
我也接受備選方案;),我只是想能夠知道一個路徑,然後通過電子郵件發送文件,不介意如何(我知道有時候人們想保持他們的代碼,但這只是一個獨立的腳本)
我還需要打開該文件在後一階段,以電子郵件發送,我得到沒有發現,我要猜一個文件從這裏開始
全功能列表(如果它幫助):
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
究竟是什麼'svn_drive',' svn_repo_path'和'relative_path'。他們是如何產生的?你爲什麼使用'.encode()'? – 2013-05-08 09:26:41
更新了與該信息的問題 – roundcrisis 2013-05-08 09:29:01
我使用編碼,因爲我在REPL中嘗試相同的東西,它的工作(即isdir返回true) – roundcrisis 2013-05-08 10:19:12