[英]fatal: destination path 'myproject' already exists and is not an empty directory
有趣的是,当我尝试使用bash shell克隆对象时
ssh -t -i ~/.ssh/security.pem root@xx.xxx.xx.xx 'rm -rf myproject && git clon -b mybranch https://github.com/myproject.git'
一切正常。
但是当我尝试从python子进程调用中执行此操作时,例如
subprocess.check_call("ssh -t -i ~/.ssh/security.pem root@xx.xxx.xx.xx 'rm -rf myproject && git clone -b mybranch https://github.com/myproject.git'", shell=True)
那么我会得到以下错误:
致命:目标路径“ myproject”已存在,并且不是空目录。
追溯(最近一次通话):
文件“”,第1行,位于
在check_call中的文件“ C:\\ Python27 \\ lib \\ subprocess.py”,第540行
提高CalledProcessError(retcode,cmd)
如果要执行长bash命令,则必须将其参数拆分为一个列表。 就像如果我想使用子流程库运行“ ls -a”一样,我将不得不做:
subprocess.call(["ls","-a"])
查看文档以供参考: https : //docs.python.org/2/library/subprocess.html
但是,如果您仍然无法删除该文件夹,则可以使用shutil库运行shutil.rmtree()
。
这是我发现的内容:shell = True。 由于某种原因,python将在远程计算机上执行第一个shell命令'rm -rf myproject',然后关闭SSH连接,最后执行第二个命令'git clone -b branch https://github.com/myproject .git '在本地计算机中。 就我而言,我在本地目录中有相同的git存储库“ myproject”,因此git尝试在本地目录中进行克隆并投诉。 更改为shell = False或将其省略后,一切正常。 不知道为什么shell值会导致这种情况?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.