[Tutor] Windows file structure

Liam Clarke cyresse at gmail.com
Sun Oct 31 02:46:00 CEST 2004


Hi Rafal

Have a look at the os module, os is designed for cross platform
compatibility, and it's handy on any OS.

As a beginner myself, here's what I learnt in dealing with Python and
directories -

Use forward slashes. Win32 uses back slashes itself, but use forward
slashes and Python does the rest.

The os module and os.path are your friends.

For example, my code that checks for a directory, and if it isn't
present, creates it is as follows-

if not os.path.exists('./archives/wb%s' % weekstart):
        os.mkdir('./archives/wb%s' % weekstart) 

os.path.exists returns a 1 if the specified path exists, 0 if it doesn't. 

the "./archives/wb%s" % weekstart is probably what you need. Say
you've got five folders to create, and three languages to do it in,
you could probably set up a list of the folder names
like 

folderlist=[["English", "data", "temporary", "main", "archive"], 
               ["Español", "datos", "temporal", "principal", "archivo"],
               ["Italiano", "dati", "provvisorio", "principale", "archivio"]]

And then a wee loop - 

for slot in range(len(folderlist)):
           os.mkdir("../%s" % folderlist[slot][0])
           for secondslot in range(1,len(folderlist[slot])):
                 os.mkdir("../%s/%s" % (folderlist[slot][0],
folderlist[slot][secondslot]))

So that will create three directories in your root directory, English,
Español, and Italiano, and each folder would have the subfolders as
above.

I will add my standard disclaimer - 

This is a quick and dirty way to do it, there's probably a simpler,
cleaner way to do it, but it works. For me. There's probably a couple
of *nix pitfalls I'm forgetting, probably to do with privileges and
what not. I don't know *nix from my foot, so yeah.... : /

Hope it helps

Liam Clarke

On Mon, 25 Oct 2004 11:13:42 +0100, Rafal Kaniewski
<rafal.kaniewski at capital-fx.co.uk> wrote:
> Hello world...
> 
> Seeking to learn by creating a solution that makes empty folders
> according to a set of variables on a windows network. Would prefer the
> solution to also work in Unix and OSX file structures.
> 
> i.e. I have a list of different languages and I need to create several
> sets of empty folders in different specified places on the network,
> folders are named in different sets acording to the languages.
> 
> Please advise where to start...Thanks...
> 
> Rafal Kaniewski
> 
> (previous experience  - some of the beginners tutorials on python.org;
> basic HTML / XML ; intermediate filemaker scripting)
> 
> Scanned for viruses by MailDefender
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list