Why is __name__ == "__main__" not working?

How would you critique my code?

  • I'm just starting to pick up Python as my first language so go easy. The program basically allows you to navigate the basic directories on your OSX machine, find a .txt file, then search for a number of times something appears in that file. This was made purely to test my ability to use the re, glob, and os modules. (I am aware I don't have to use the specific names of variables to push data through my find(mat, ftemp) functions, just do it for legibility sake) (I have not learned classes yet) Questions: Did I use '\n' in the correct way to style this? Why do I find my self having to make a copy of "f" per use? if I use the same f from main(), the list from text appears empty? What would be a better way to make the settext() function? (I couldn't  quite figure out how to set something = to f.read() again nothing appeared when would try to print it) How do I make it easier for my client, in the sense that they don't have to put in the '' on each input? feel free to answer unasked questions. Thanks! (Day 3ish of Python) ***UPDATED*** import os, glob, re def user(): print '\n' user = raw_input('What is the nickname of this user account? ') print '\n'*2, 'Thank you ', user + '.', '\n' return user def get(): print '\n'*3, 'Current Dir: ', os.getcwd(), '\n' print 'Contents of Dir: ', '\n', os.listdir('.'), '\n' print 'Contents of New Dir (.txt only): ', '\n', glob.glob('*.txt'), '\n' newdir = raw_input("What is the new dir? (press enter if in proper dir) ") while newdir <> '': os.chdir(newdir) print '\n', 'New Dir: ', os.getcwd(), '\n' print '\n', 'Contents of Dir (all):', '\n', os.listdir('.'), '\n' print 'Contents of New Dir (.txt only): ', '\n', glob.glob('*.txt'), '\n' newdir = raw_input("What is the new dir? (press enter if in proper dir) ") ftemp = raw_input('What is the file you would like to see? ') print '\n' fil = open(ftemp, 'r') f = '' for line in fil: f = f + str(line) return f, ftemp def filesize(ftemp): size = os.stat(ftemp).st_size size = float(size) / 1000 print '\n' + ftemp + "'s size is: " + str(size) + ' KB' def show(f): print 'Now going to read the file... ', '\n' print '-----------------------------------', '\n' print f print '\n', '-----------------------------------', '\n' def Find(f, ftemp): print '\n' w = raw_input('What are you searching for? ') match = re.findall(w, f) if match: x = 0 for i in match: x = x + 1 print '\n', 'Results: found '+ "'" + w + "' in: " + ftemp + ' ' + str(x) + ' times.', '\n' else: print '\n', 'NOT FOUND!', '\n' print '\n' def main(): un = user() os.chdir('/Users/' + un) f, ftemp = get() filesize(ftemp) show(f) Find(f, ftemp) if __name__ == '__main__': main()

  • Answer:

    Have you thought about using os.walk()? You should be able to clean up your code quite a bit if you re-factor to include os.walk().

Michael Lewis at Quora Visit the source

Was this solution helpful to you?

Other answers

Use raw_input instead of input.

Seo Sanghyeon

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.