简体   繁体   English

致命:目标路径“ myproject”已存在,并且不是空目录

[英]fatal: destination path 'myproject' already exists and is not an empty directory

interesting enough, when I tried to clone object using bash shell 有趣的是,当我尝试使用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'

everything works beautifully. 一切正常。

But when I tried to do it from python subprocess call, like 但是当我尝试从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)

then I will get the following error: 那么我会得到以下错误:

fatal: destination path 'myproject' already exists and is not an empty directory. 致命:目标路径“ myproject”已存在,并且不是空目录。
Traceback (most recent call last): 追溯(最近一次通话):
File "", line 1, in 文件“”,第1行,位于
File "C:\\Python27\\lib\\subprocess.py", line 540, in check_call 在check_call中的文件“ C:\\ Python27 \\ lib \\ subprocess.py”,第540行
raise CalledProcessError(retcode, cmd) 提高CalledProcessError(retcode,cmd)

If you are doing a long bash command, you have to split up its arguments into a list. 如果要执行长bash命令,则必须将其参数拆分为一个列表。 Like if I wanted to run "ls -a" using the subprocess library I would have to do: 就像如果我想使用子流程库运行“ ls -a”一样,我将不得不做:

subprocess.call(["ls","-a"])

Check out the docs for reference: https://docs.python.org/2/library/subprocess.html 查看文档以供参考: https : //docs.python.org/2/library/subprocess.html

But, if you still have trouble removing the folder, shutil.rmtree() will work, using the shutil library. 但是,如果您仍然无法删除该文件夹,则可以使用shutil库运行shutil.rmtree()

Here is what I find out: shell=True. 这是我发现的内容:shell = True。 For some reason, the python will execute the first shell command 'rm -rf myproject' in the remote machine and then close the SSH connection, finally to execute the second command 'git clone -b branch https://github.com/myproject.git ' in the local machine. 由于某种原因,python将在远程计算机上执行第一个shell命令'rm -rf myproject',然后关闭SSH连接,最后执行第二个命令'git clone -b branch https://github.com/myproject .git '在本地计算机中。 In my case, I have the same git repository 'myproject' in my local directory, so git tries to clone in my local directory and complains it. 就我而言,我在本地目录中有相同的git存储库“ myproject”,因此git尝试在本地目录中进行克隆并投诉。 After changing to shell=False, or leave it out, then everything works. 更改为shell = False或将其省略后,一切正常。 Not sure about why the shell value can cause that?! 不知道为什么shell值会导致这种情况?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM