【问题标题】:fatal: destination path 'myproject' already exists and is not an empty directory致命:目标路径“myproject”已经存在并且不是空目录
【发布时间】:2015-09-26 20:04:15
【问题描述】:

有趣的是,当我尝试使用 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 行,在
文件“C:\Python27\lib\subprocess.py”,第 540 行,在 check_call
引发 CalledProcessError(retcode, cmd)

【问题讨论】:

  • 运行check_call("ssh remote-host 'hostname && hostname'", shell=True) 或(更好)check_call(["ssh", "remote-host", "hostname && hostname"]) 以查看两个命令都在远程主机上执行。 Python 不是问题,问题在于您的实际命令(不是您显示的命令)——检查是否有杂散的单引号。

标签: python git ssh subprocess


【解决方案1】:

如果您正在执行长 bash 命令,则必须将其参数拆分为一个列表。就像我想使用子进程库运行“ls -a”一样,我必须这样做:

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

查看文档以供参考:https://docs.python.org/2/library/subprocess.html

但是,如果您仍然无法删除文件夹,shutil.rmtree() 可以使用 shutil 库。

【讨论】:

    【解决方案2】:

    这是我发现的:shell=True。由于某种原因,python会在远程机器上执行第一个shell命令'rm -rf myproject'然后关闭SSH连接,最后在本地机器上执行第二个命令'git clone -b branchhttps://github.com/myproject.git'。就我而言,我的本地目录中有相同的 git 存储库“myproject”,因此 git 尝试在我的本地目录中克隆并抱怨它。更改为 shell=False 后,或将其排除在外,然后一切正常。不确定为什么 shell 值会导致这种情况?!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多