Score:0

how to create multiple subfolders in pyhton using subprocess?

us flag
CST

On my ubuntu 20.04 when I use

mkdir -p /home/parent/{child1,child2}

in my terminal I get a folder parent with 2 subfolders child1 and child2.

  • parent
  • parent/child1
  • parent/child2

However, when I try to run this in a python script using

bashCommand = "mkdir -p /home/parent/{child1,child2}"
#print(bashCommand)
subprocess.run(bashCommand.split(), capture_output=True)

I end up having a parent folder and one child folder named {child1,child2}.

  • parent
  • parent/{child1,child2}

How can I fix this?

pLumo avatar
in flag
Don't use `subprocess`, but `os.mkdir()` or `os.makedirs()`.
muru avatar
us flag
I recommend that too, but for the general case: https://stackoverflow.com/questions/22659579/curly-braces-in-python-popen
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.