Creating a folder structure getting arguments

Hi guys, I have a question about using argv.

Let’s say as a user I want to run a script that creates folders.
I want to grab the arguments, iterate over them and create a folder.

The idea is to use argv.

I have researched about doing it using:

import os
path=
os.chdir(path)
for i in range(1,11):
     NewFolder = "Folder" + str(i)
     os.makedirs(Newfolder)

but the idea is only using arg and not range, I think is more like running the script on cmd and also defining the path for the user to input the folder names and creates the folder on the location.

I’m still learning this, I’m trying to make sense of what I want to do :smiley: sorry if it is confusing.

You’ve got the right idea. Try this in a python shell:

range(1, 11)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

See how it prints out a list with 1-10? All you’re doing is going through each element of that list and assigning it to i.

argv is also a list, so you just do the same thing:


for dirname in sys.argv[1:]:
   # do you stuff here

But, you might need to see what sys.argv[1:] does first. The [:] is a way to slice the list from 1 to the end. Does that remove the python script from the list? Print it out first and see what it does.

1 Like
A free service run by Zed A. Shaw for learncodethehardway.org.