简体   繁体   English

在 centos 7 中运行的结构:mkdir 在控制台中工作但不在 fabfile.py 中

[英]fabric running in centos 7: mkdir working in console but not in fabfile.py

I have the following code in my fabfile.py (Centos 7 machine):我的 fabfile.py(Centos 7 机器)中有以下代码:

def deploy(version):

    env.directory = os.path.join(env.config['home'], version, 've')
    env.activate = os.path.join(env.directory, 'bin/activate')
    env.version = os.path.join(env.config['home'],version)
    print('this is the home dir')
    print(env.config['home'])
    print(env.version)
    run('mkdir -v {}'.format(env.version))

On running i received the following error:在运行时,我收到以下错误:

[mcvitty] Executing task 'deploy'
this is the home dir
/home/mcvitty
/home/mcvitty/3.6.3
[mcvitty] run: mkdir -v /home/mcvitty/3.6.3
[mcvitty] Login password for 'webapp': 
[mcvitty] out: mkdir: cannot create directory ‘/home/mcvitty/3.6.3’: File exists
[mcvitty] out: 


Fatal error: run() received nonzero return code 1 while executing!

Requested: mkdir -v /home/mcvitty/3.6.3
Executed: /bin/bash -l -c "mkdir -v /home/mcvitty/3.6.3"

But if I run the culprit line on the console the mkdir works fine:但是,如果我在控制台上运行罪魁祸首行,则 mkdir 工作正常:

/bin/bash -l -c "mkdir -v /home/mcvitty/3.6.3"
mkdir: created directory ‘/home/mcvitty/3.6.3’

What's wrong with my code?我的代码有什么问题?

I would say: don't use external commands to create a directory when you can do it with python.我会说:当你可以用 python 创建目录时,不要使用外部命令。 Most probably the directory exists for some reason when you're running your script.当您运行脚本时,该目录很可能出于某种原因存在。 Then it's deleted.然后就删了。 Doesn't matter much.没有太大关系。

To avoid failure if the directory exists, just test if exists before creating it.如果目录存在,为避免失败,只需在创建之前测试是否存在。

if not os.path.isdir(env.version):
   os.mkdir(env.version)
   print("Created directory {}".format(env.version)

Portable and safe.便携且安全。

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

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