[Tutor] New to pythong

FT chester_lab at fltg.net
Mon Jul 7 22:26:08 CEST 2008


Hello everybody:

I am new to this mailing list, and it said that i could the simplest of questions. So i was wondering if anyone could be so kind as to e-mail me a project idea or something to go out an learn to do in python. I don't know any languages, but i am definitely not computer illiterate. i have read so many tutorial about getting started but so far that is where the tutorial have left me ( how to print  "Hello World") and such. 

Any ideas great thanks.

Hi!

    Someone posted the description on how to fid and list files. This is one you could look at, study, and then expand on it. I added the file search as well.

    In each module you can expand and do much more with it, but when dealing with files and directories you must remember that you can erase a lot of stuff if you're not careful.


    Enjoy, Bruce



import sys, os   # imports all the functions in the os module (operating system functions)
cwd = os.getcwd()   # gets the current working directory and places it in the variable cwd
print "Enter file to search for: "
file2find = str(raw_input("__>"))
if file2find=="":
    sys.exit()
dirFiles = open( "filelist.txt","w")  # creates the file filelist.txt ready for writing using the variable dirFiles
# the walk function returns the current directory in root, the directories in dirs
# and the files in root in files. By using the for loop it traverses every folder
# from the starting folder.
c=0  #Count the files found!
for root, dirs, files in os.walk(cwd):
#    dirFiles.write(root + "\n")   # writes to dirFiles.txt the current directory name
    i=0   #file count for directory to save directory path if found!
    for theFiles in files:      # will iterate all the files in the current directory
        if file2find in theFiles:
            i+=1
            c+=1
            if i==1:
                dirFiles.write(root + "\n")   # writes to dirFiles.txt the current directory name
            dirFiles.write( "___________ " + theFiles + "\n") # writes the filename to dirlist.txt. the
dirFiles.close() # Cleanly saves and closes the file dirlist.txt
# Note: the "\n" in the write lines adds the newline character so that the next write starts on a newline


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080707/30eca10b/attachment-0001.htm>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: DirWalk4File.py
URL: <http://mail.python.org/pipermail/tutor/attachments/20080707/30eca10b/attachment-0002.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: DirWalk.py
URL: <http://mail.python.org/pipermail/tutor/attachments/20080707/30eca10b/attachment-0003.txt>


More information about the Tutor mailing list