[Tutor] Noob to Python - Need help with Variables

Magnus Lyckå magnus@thinkware.se
Sat Jun 7 21:32:01 2003


At 18:10 2003-06-05 +0100, Tony Stevenson wrote:
>os.mkdir("D:\\tempint\\" + user)

You can use forward slashes in Windows. Just not at the command
prompt, where they will be confused with command options. So,
"D:/tempint/" instead of "D:\\tempint\\" will work. Another, more
generic way to avoid writing double backslashes is to use raw
strings. This is only possible if you don't need to expand any
escape sequences such as \t into a tab, but in this case, you
could use r"D:\tempint\".

Finally, if you want a solution that works across operating
systems and have something like

d = "directory"
s = "subdirectoy"

Then you might do d + '\\' s in windows, d + '/' + s in unix
and d + '::' + s or something like that in other operating
systems.

Don't! Use:

import os.path # Just "import os" works too, but imports more
full_path = os.path.join(d, s)

So, in your case, you could do

d = r"D:\tempint"
os.mkdir(os.path.join(d, user))


--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language