[Tutor] tips for writing a program

Alan Gauld alan.gauld at btinternet.com
Wed Jun 20 11:08:17 CEST 2007

"Mat Newport" <lake2snow19 at hotmail.com> wrote

> Basically the program would scan a specified folder, 
> build a list of specific files (.txt in the example below), 

Look at the glob module and its glob function.

Or if you want to include subdirectories lkook at the 
os.walk function. There are examples of using both 
in my Using the OS topic in my tutorial.

> ... to move and rename certain one's 

And the os and shutil modules contain functions to do 
this - again see my OS topic for examples.

> based on a separate list. 

Which could be stored in a file and read into a 
normal python list at startup.

> it would then test its list of found files against 
> a large database for a match, and upon finding 
> the match copy move and rename them 

All fairly simple using the modules mentioned above.

> Et cetera. I know basic python but don't know how to make 
> it generate the list, 

glob will give you a list, os.walk needs more work 
but the normal Python list processing methods will 
work just fine. Do you have any more specific 

> store them, 

You probably only need to store them in memory so 
once you put them in a list you should be fine.

> analyze it against another list, 

That can be done by looping over one list while comparing 
the items in the other. Luke has shown one way to do that 
using a list comprehension.

> then copy and rename based on the matches it finds. 

See the shutil functions mentioned above.

> Is this a task for a different program?

No, Python is perfectly suited to this kind of task. 
I did something very similar a few weeks ago

Have a go, if you get stuck come back and ask us more 
specific questions and show us the code and error messages.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list