0

How to automatically create 5 sub-directories using Python on Ubuntu?

i tried to create a .txt file with the folders name in it, than tried to execute the following command but its not working.

python -c 'import sys,os,codecs;[os.mkdir(d) for d in codecs.open(sys.argv[1],"r",&q­uot;utf8")]' subdirectory.txt

i believe this command will create folders in 1 folder instead of creating sub directories within a folder so i am wrong there

I am currently trying to perform this action on my Virtual box on my Ubuntu 12.04.

i would like to see my output to be

/home/user01/testdirectory/subdirectory
/home/user01/testdirectory/subdirectory/sub1
/home/user01/testdirectory/subdirectory/sub1/sub2
/home/user01/testdirectory/subdirectory/sub1/sub2/sub3

and so on

7
  • 4
    Try to use os.makedirs() instead of os.mkdir Commented Nov 29, 2015 at 7:28
  • @Alex I tried exactly the same thing using IDLE (Python 3.5 32-bit) on my windows machine and it works successfully. But on Ubuntu its throwing some errors Commented Nov 29, 2015 at 7:32
  • 1
    @omar89: What's the error? Commented Nov 29, 2015 at 7:45
  • on IDLE i used >>> import os >>> os.makedirs('dir1/dir2/dir3') >>> os.getcwd() The above works only on IDLE and creates sub directories on my computer, but on Ubuntu even this keeps throwing errors Commented Nov 29, 2015 at 7:46
  • @KevinGuan SyntaxError: invalid syntax i see "^" below the & sign before "quot" in the code Commented Nov 29, 2015 at 7:56

2 Answers 2

1

The " is obviously not valid Python. I guess you copied this from a web site with broken HTML coding. The correct syntax is

codecs.open(sys.argv[1],"r","utf8")
Sign up to request clarification or add additional context in comments.

1 Comment

I'd suggest also add use os.makedirs() instead of os.mkdir() as Alex's comment in the answer.
1

Not sure I understand you correctly. Do you want to create just a long path of directories in one shoy? If so, it can be achieved by os.makedirs('/long/path/is/here').

If you want to create a bunch of directories before creating file you could use os.path.dirname to get a directory, create a path, and then create a file.

If you need to create a bunch of directories taken from file then:

cat textfile.txt | xargs -n 1 python -c 'import os, sys;os.makedirs(sys.argv[1])'

even better. You don't need python.

cat file.txt | xargs -n 1 mkdir -p

2 Comments

The cat is useless. xargs has an -a option to read from a file if redirection is somehow inconvenient.
@Volodymyr i fixed the directory issue. Now after executing the first command provided by you, It is creating sub directories under my main directory. TO fix my .txt file i change folders names on each lines to dir1/dir2/dir3 Thanks alot for all the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.