Home > Blockchain >  subprocess.run() issues with mkdir
subprocess.run() issues with mkdir

Time:10-29

I am having trouble with subprocess.run().

I try to create a new directory in a WindowsSubsystemForlinux1 WSL1 application inside W10.

The directory name shall be (for example): a0_998.0784269595375 (always 13 digits after the comma). That set of digits comes as the output of a Python function. What I have:

def SMILEI(I):
    os.chdir("/home/velenos14/PICsims/github/SMILEI_correctTunnelBSIrate/Smilei/SIMRESULTS/GPs_trial_Xenon_noOAM")
    a0 = "%.13f" % a0_from_IntensityWcm2(I) # a0_from_IntensityWcm2() is a function
    subprocess.run(['dirname="a0_"', 'mkdir' '-p' '$dirname'], check=True, text=True) # PermissionError: [Errno 13] Permission denied: 'dirname="a0_'

What I am doing wrong? The BASH commands (which work and do the job) are as following:

for ((i = 1; i <= 15000; i  ))
do
    index=$((i))
    a0=$(awk "NR==${index} { print \$2 }" Intensity_Wcm2_versus_a0_10_20_10_25_range.txt)
    dirname="a0_${a0}"
    mkdir -p $dirname
    cd $dirname
done

CodePudding user response:

As far as I can see you want to pass the dirname argument as an environment variable to your mkdir subprocess.

Instead construct your dirname variable beforehand and pass it to subprocess.run like this:

def SMILEI(I):
    os.chdir("/home/velenos14/PICsims/github/SMILEI_correctTunnelBSIrate/Smilei/SIMRESULTS/GPs_trial_Xenon_noOAM")
    dirname = "a0_%.13f" % a0_from_IntensityWcm2(I)
    subprocess.run(["mkdir", "-p", dirname], check=True, text=True)
  • Related