2012-12-28 87 views
0

我有下面的代码,我试图检查目录“Gerrits/HEAD/wlan”,然后做一些操作,由于某种原因,如果检查目录的条件仍然失败甚至认为目录是否存在? 什么不对的地方,如果条件@如果(os.path.isdir(SCRIPT_ROOT + “/ Gerrits/HEAD/WLAN”)):下面os.path.isdir检查失败

import os 
import subprocess 
from subprocess import check_call 

SCRIPT_ROOT=subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0] 
print SCRIPT_ROOT 

def main(): 
    if (os.path.isdir(SCRIPT_ROOT + "/Gerrits/HEAD/wlan")): 
     print "SCRIPT_ROOT/Gerrits/HEAD/wlan already exists,cloning it again to the tip" 
     check_call("rm -rf $SCRIPT_ROOT/Gerrits/HEAD/wlan ", shell=True) 
     check_call("cd Gerrits/HEAD",shell=True) 
    else: 
     print "SCRIPT_ROOT/Gerrits/HEAD/wlan doesn't exist,cloning it" 
     os.makedirs("Gerrits/HEAD/wlan") 
     check_call("cd Gerrits/HEAD",shell=True) 
     currdir=subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0] 

if __name__ == '__main__': 
    main() 

错误: -

SCRIPT_ROOT/Gerrits/HEAD/wlan doesn't exist,cloning it 
Traceback (most recent call last): 
    File "test.py", line 21, in <module> 
    main() 
    File "test.py", line 16, in main 
    os.makedirs("Gerrits/HEAD/wlan") 
    File "/usr/lib/python2.6/os.py", line 157, in makedirs 
    mkdir(name, mode) 
OSError: [Errno 17] File exists: 'Gerrits/HEAD/wlan' 
+0

请在询问前缩小问题范围。这是太多东西来通过。 –

+0

@JeremyVisser - 多数民众赞成正是我所做的,请重新 – user1927396

回答

1

添加.strip()到您的communicate()[0]调用,代码原样包括输出中的尾随换行符。

可以肯定的是,我刚刚在Python 2.5的Linux机器上测试过的脚本。

import os 
import subprocess 
from subprocess import check_call 

SCRIPT_ROOT=subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0].strip() 
print SCRIPT_ROOT 

def main(): 
    if (os.path.isdir(SCRIPT_ROOT + "/Gerrits/HEAD/wlan")): 
     print "SCRIPT_ROOT/Gerrits/HEAD/wlan already exists,cloning it again to the tip" 
     check_call("rm -rf %s/Gerrits/HEAD/wlan" % SCRIPT_ROOT, shell=True) 
     check_call("cd Gerrits/HEAD",shell=True) 
    else: 
     print "SCRIPT_ROOT/Gerrits/HEAD/wlan doesn't exist,cloning it" 
     os.makedirs("Gerrits/HEAD/wlan") 
     check_call("cd Gerrits/HEAD",shell=True) 
     currdir=subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0].strip() 

if __name__ == '__main__': 
    main() 

,其输出:

[email protected]:~$ python o.py 
/media/home/vlazarenko 
SCRIPT_ROOT/Gerrits/HEAD/wlan already exists,cloning it again to the tip 
+0

甚至加入.strip(后连读),它仍然认为它不存在 – user1927396

+0

粘贴完整的脚本,做我的工作环境的变化,给予我转载'Gerrits/HEAD的存在/ wlan'在我的homedir。 – favoretti

+0

我切换到os.getcwd(),但现在的删除失败@“check_call(”室射频$ SCRIPT_ROOT/Gerrits/HEAD/WLAN“壳=真)”未能......如何改变这种RM声明删除整个“Gerrits/HEAD/wlan”(如果它存在)... – user1927396

1

当我在这里执行这行代码:

SCRIPT_ROOT=subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0] 

...的SCRIPT_ROOT价值有一个结尾的新行

>>> import os 
>>> import subprocess 
>>> ROOT = subprocess.Popen(['pwd'], stdout=subprocess.PIPE).communicate()[0] 
>>> ROOT 
'/Users/bgporter/personal\n' 

...这使得这个致电

if (os.path.isdir(SCRIPT_ROOT + "/Gerrits/HEAD/wlan")): 

行为与您希望的不同。您可以在该值调用带(),或者如果你总是希望得到当前工作目录,您可以通过调用os.getcwd()

同样做到这一点更容易,你可以使用os.removedirs()功能递归删除目录你不想要掏腰包。

+0

我改os.getcwd但不知何故,脚本仍然认为该目录犯规存在 – user1927396

+0

我认为现在的“check_call(”室射频$ SCRIPT_ROOT/Gerrits/HEAD/WLAN“壳=真)”失败...如何更改此rm语句以删除整个“Gerrits/HEAD/wlan”(如果它存在)... – user1927396