[Tutor] tips for writing a program

Luke Paireepinart rabidpoobear at gmail.com
Wed Jun 20 04:36:46 CEST 2007


Mat Newport wrote:
> I have taken a fundamentals of programming course at a local college, but that class does not provide the level of expertise to write a program I have in mind. So I do have basic skills, I just need to be shown the tools to write this thing and I think I could put it all together. 
>   
You sound confident in your ability to reason the problem out.  That's 
very good.
> Basically the program would scan a specified folder, build a list of specific files (.txt in the example below), and then have the ability to move and rename certain one's based on a separate list. For example, it would scan c:\test\  for .txt files
> and find:
>
> "Koontz lighning.txt"
> "davinci_code.txt" 
>
> 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 to
>
> "Dean Koontz - Lightning.txt"
> "Dan Brown - The Davinci Code.txt"
>
> Et cetera. I know basic python but don't know how to make it generate the list, store them, analyze it against another list, then copy and rename based on the matches it finds. Is this a task for a different program?
>   
There are certainly automated file renamer programs out there, but none 
can provide the flexibility of being able to code your own.
Also, if you're really interested in programming, you should take any 
opportunity you can to work on your skills, provided the required level 
of skill
is not vastly out of reach of your own level at the time, and from what 
you have said, I believe you could do this program.

I'm not exactly sure what all your questions are... if you explain in 
more detail, we should be able to assist you with everything.
Here's some hints for what I think you're asking.

"how to generate the list" of files in a directory.
import os #we need this for the path and directory listing functions
directory = 'c:\\test' #define the target directory
extensions = ['txt']
#a list comprehension would be a succinct way to filter the directory 
listing.
listing = [filename for filename in os.listdir(directory) if 
os.path.splitext(filename)[-1].lower() in extensions]

listing now contains all the filenames in the target directory that 
contain the file extension you're looking for.
If the above line doesn't make sense, you can try reading about "list 
comprehensions."  feel free to ask any questions you might have.


"how to store them"
I'm assuming you mean the directory listing?
I'm not sure what you mean by 'store'
You mean permanently store?

"analyze it against another list"
I'm not sure where these lists are coming from.
If you have a basic, direct mapping from incorrect to correct filenames,
like  you know
Koontz lighning.txt  should always be renamed to Dean Koontz - Lightning.txt
the problem is fairly simple.
If you're trying to match based on keywords, etc. it gets more complex.

"copy and rename based on the matches it finds"
if you want to rename a file, you can use the os.rename function.
If you ever find you need help on a certain function,
just use the builtin method "help"
 >>> help(os.rename)
Help on built-in function rename in module nt:

rename(...)
    rename(old, new)
   
    Rename a file or directory.

so this tells us we can do, for example,

os.rename("C:\\test.txt","C:\\foobar.txt")


So try to be more specific where I got confused (I get confused easily)
and I'll try to give you more specific help.
-Luke

>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   



More information about the Tutor mailing list