Issue
I have the following code in my fabfile.py (Centos 7 machine):
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:
/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?
Solution
I would say: don't use external commands to create a directory when you can do it with 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.
Answered By - Jean-François Fabre Answer Checked By - Gilberto Lyons (WPSolving Admin)