critique this little script, if you like

J

John Salerno

# Create new subdomain directory (with subdirectories) in my 1and1 folder

import os
import sys

subdirs = [r'\cgi-bin', r'\images', r'\styles']

#if len(sys.argv) == 1:
# subdomain = raw_input('Enter subdomain name: ')
#else:
# subdomain = sys.argv[1]

try:
subdomain = sys.argv[1]
except IndexError:
subdomain = raw_input('Enter subdomain name: ')

path = r'C:\Documents and Settings\John Salerno\My Documents\My
Webs\1and1\johnjsalerno.com\\' + subdomain

os.mkdir(path)
for subdir in subdirs:
os.mkdir(path + subdir)


Just a simple script I wrote as an experiment, and it works nicely
anyway. My two questions are these:

1. Like my other post, I'm wondering if it's okay to handle the ending
backlash as I did in path

2. Between the if block or the try block, which is more Pythonic? The
try block seems nicer because it doesn't have such an ugly-looking check
to make.
 
D

Dave Jones

Hi John!

About the path and the \'s, take a look at the os.path.join function.
The function is smart enough to add the correct slash when joining
directories.

Dave
 
J

John Salerno

Dave said:
Hi John!

About the path and the \'s, take a look at the os.path.join function.
The function is smart enough to add the correct slash when joining
directories.

Dave

Interesting, thanks! I was reading about the normcase() function, but
your suggestion might be better.
 
J

John Salerno

John said:
Interesting, thanks! I was reading about the normcase() function, but
your suggestion might be better.

Excellent, works great! But one more question: do I need to import
os.path for this? I tried it with and without it, and it worked both
ways, so I'm a little uncertain about whether os.path is really a
separate module, or if it is still contained within os and doesn't need
to be imported separately (it seems like it doesn't, but it is listed
separately in the docs).
 
A

Alexis Roda

John Salerno escribió:
2. Between the if block or the try block, which is more Pythonic?

Since the command line argument is optional I don't think it should be
considered and exceptional condition if it's missing, so the "if" block
looks better to me. No idea if this is more pythonic.
> The
> try block seems nicer because it doesn't have such an ugly-looking check
> to make.

Ok, so you should do:

try:
x = 1/i
except ZeroDivisionError :
do_something_if_zero()

instead of

if (i == 0) :
do_something_if_zero()




HTH
 
R

Roel Schroeven

Dave Jones schreef:
Hi John!

About the path and the \'s, take a look at the os.path.join function.
The function is smart enough to add the correct slash when joining
directories.

But watch out with leading backslashes, as in

subdirs = [r'\cgi-bin', r'\images', r'\styles']

os.path.join will assume they are meant to be absolute paths, and will
discard all previous components:
'\\images'

In fact I think it's best to specify components without leading or
trailing backslashes (unless you really want an absolute path):
'base\\subdomain\\images'
 
R

Roel Schroeven

John Salerno schreef:
Excellent, works great! But one more question: do I need to import
os.path for this? I tried it with and without it, and it worked both
ways, so I'm a little uncertain about whether os.path is really a
separate module, or if it is still contained within os and doesn't need
to be imported separately (it seems like it doesn't, but it is listed
separately in the docs).

I always thought I had to import it separately, but just yesterday I
discovered import os and import os.path do the same thing. If I do one
of the two, doesn't matter which one, I can access both os.listdir and
os.path.join.
 
J

John Salerno

Roel said:
But watch out with leading backslashes, as in

subdirs = [r'\cgi-bin', r'\images', r'\styles']

os.path.join will assume they are meant to be absolute paths, and will
discard all previous components:
'\\images'

In fact I think it's best to specify components without leading or
trailing backslashes (unless you really want an absolute path):
'base\\subdomain\\images'

Yeah, I made the list of subdirectories just strings, like 'cgi-bin' and
took out the backslash. Then I used os.path.join to create those
subdirectories on the end of the main directory. It always amazes me how
posting here can lead to little tweaks that really clean up my code. :)
 
J

John Salerno

Alexis said:
John Salerno escribió:

Since the command line argument is optional I don't think it should be
considered and exceptional condition if it's missing, so the "if" block
looks better to me. No idea if this is more pythonic.


Ok, so you should do:

try:
x = 1/i
except ZeroDivisionError :
do_something_if_zero()

Why do you think that this try block is preferred over what I have (if I
were to stick with the try block)?
 
A

Alexis Roda

John Salerno escribió:
Why do you think that this try block is preferred over what I have (if I
were to stick with the try block)?

My example has nothing to do with your script, apart from trying to
illustrate why I think you should use "if" instead of "try" with a kind
of "reductio ad absurdum" reasoning.

To me i == 0 seems as ugly as len(sys.argv) == 1 (*), so, according to
your statement about ugly checks, you should avoid code like:

if i == 0 :
do_something_if_zero()

and write something like:

try:
x = 1 / i
except ZeroDivisionError :
do_something_if_zero()

but probably you wouldn't, the "if" block seems more "natural" than the
convoluted "try" block, so ...




Regards

(*) len(sys.argv) == 1 is exactly the same as len(sys.argv) - 1 == 0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top