Re: [Tutor] Class methods
"David Merrick" wrote class Critter(object): def __init__(self, name, hunger = 0, boredom = 0): def __pass_time(self): def __str__(self): @property def mood(self): def talk(self): def eat(self): def play(self): class Farm(Critter): I still don't think a Farm is a type of Critter... def __init__(self,farmlet): Critter.__init__(self,farmlet) This will set the name to farmlet, which I don't think you want. def talk(self,farmlet): You don't need to pass farmlet in since the class has farmlet stored inside it. You can access farmlet with self.farmlet. for critter in farmlet: print("Hello") Critter.talk(farmlet) You want the instance to talk not the class. So you need to use critter.talk() And the talk method does not take any arguments except self. What you are doing here is calling the class method with an instance value of farmlet. ie self in that method gets the value of farmlet. def main(): crit1 = Critter("Sweetie") crit2 = Critter("Dave") farmlet = [crit1,crit2] f = Farm(farmlet) choice = None while choice != "0": print \ (""" Critter Caretaker 0 - Quit 1 - Listen to your critter 2 - Feed your critter 3 - Play with your critter """) choice = input("Choice: ") print() # exit if choice == "0": print("Good-bye.") # listen to your critter elif choice == "1": f.talk(farmlet) # feed your critter elif choice == "2": f.eat(farmlet) Note that f.eat is a method you inherit from Critter. The Critter method does not take any arguments so this will fail. # play with your critter elif choice == "3": f.play(farmlet) Same with f.play() Traceback (most recent call last): File "D:/David/Python/programs/critter_farm3.py", line 72, in talk Critter.talk(farmlet) File "D:/David/Python/programs/critter_farm3.py", line 38, in talk print("I'm", self.name, "and I feel", self.mood, "now.\n") AttributeError: 'list' object has no attribute 'name' This is because you are accessing the method via the class and passing farmlet as the instance value rather than sending the message to the innstance directly. Use critter.talk() # and no farmlet needed! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sftp get single file
You could use the subprocess module to run the relevant system commands. More info on running sftp non-interactively (i.e from a script) can be found here: http://fixunix.com/ssh/238284-non-interactive-sftp-put.html Regards Peter Lavelle ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 88, Issue 89
Can someone show me how to code this correctly please On Wed, Jun 22, 2011 at 10:00 PM, wrote: > Send Tutor mailing list submissions to >tutor@python.org > > To subscribe or unsubscribe via the World Wide Web, visit >http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to >tutor-requ...@python.org > > You can reach the person managing the list at >tutor-ow...@python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Re: Class methods (Alan Gauld) > 2. Re: sftp get single file (Peter Lavelle) > > > -- > > Message: 1 > Date: Wed, 22 Jun 2011 08:58:03 +0100 > From: "Alan Gauld" > To: tutor@python.org > Subject: Re: [Tutor] Class methods > Message-ID: > Content-Type: text/plain; format=flowed; charset="UTF-8"; >reply-type=original > > "David Merrick" wrote > > > class Critter(object): > > > >def __init__(self, name, hunger = 0, boredom = 0): > >def __pass_time(self): > >def __str__(self): > >@property > >def mood(self): > >def talk(self): > >def eat(self): > >def play(self): > > > > class Farm(Critter): > > I still don't think a Farm is a type of Critter... > > >def __init__(self,farmlet): > > Critter.__init__(self,farmlet) > > This will set the name to farmlet, which I don't > think you want. > > >def talk(self,farmlet): > > You don't need to pass farmlet in since the > class has farmlet stored inside it. > You can access farmlet with self.farmlet. > > >for critter in farmlet: > >print("Hello") > >Critter.talk(farmlet) > > You want the instance to talk not the class. > So you need to use critter.talk() And the talk > method does not take any arguments except > self. What you are doing here is calling the > class method with an instance value of farmlet. > ie self in that method gets the value of farmlet. > > > def main(): > >crit1 = Critter("Sweetie") > >crit2 = Critter("Dave") > >farmlet = [crit1,crit2] > >f = Farm(farmlet) > > > >choice = None > >while choice != "0": > >print \ > >(""" > >Critter Caretaker > > > >0 - Quit > >1 - Listen to your critter > >2 - Feed your critter > >3 - Play with your critter > >""") > > > >choice = input("Choice: ") > >print() > > > ># exit > >if choice == "0": > >print("Good-bye.") > > > ># listen to your critter > >elif choice == "1": > >f.talk(farmlet) > > > ># feed your critter > >elif choice == "2": > >f.eat(farmlet) > > Note that f.eat is a method you inherit from Critter. > The Critter method does not take any arguments > so this will fail. > > ># play with your critter > >elif choice == "3": > >f.play(farmlet) > > Same with f.play() > > > Traceback (most recent call last): > > File "D:/David/Python/programs/critter_farm3.py", line 72, in talk > >Critter.talk(farmlet) > > File "D:/David/Python/programs/critter_farm3.py", line 38, in talk > >print("I'm", self.name, "and I feel", self.mood, "now.\n") > > AttributeError: 'list' object has no attribute 'name' > > This is because you are accessing the method via > the class and passing farmlet as the instance value > rather than sending the message to the innstance > directly. Use > > critter.talk() # and no farmlet needed! > > HTH, > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > > > -- > > Message: 2 > Date: Wed, 22 Jun 2011 10:07:53 +0100 > From: Peter Lavelle > To: tutor@python.org > Subject: Re: [Tutor] sftp get single file > Message-ID: <4e01b0e9.8000...@solderintheveins.co.uk> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > You could use the subprocess module to run the relevant system commands. > More info on running sftp non-interactively (i.e from a script) can be > found here: http://fixunix.com/ssh/238284-non-interactive-sftp-put.html > > Regards > > Peter Lavelle > > > -- > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > > End of Tutor Digest, Vol 88, Issue 89 > * > -- Dave Merrick merrick...@gmail.com Ph 03 3423 121 Cell 027 3089 169 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 88, Issue 89
> > Can someone show me how to code this correctly please > > > class Critter(object): > > > >def __init__(self, name, hunger = 0, boredom = 0): > >def __pass_time(self): > >def __str__(self): > >@property > >def mood(self): > >def talk(self): > >def eat(self): > >def play(self): > > > > class Farm(Critter): > > I still don't think a Farm is a type of Critter... > > >def __init__(self,farmlet): > > Critter.__init__(self,farmlet) > > This will set the name to farmlet, which I don't > think you want. > > >def talk(self,farmlet): > > You don't need to pass farmlet in since the > class has farmlet stored inside it. > You can access farmlet with self.farmlet. > > >for critter in farmlet: > >print("Hello") > >Critter.talk(farmlet) > > You want the instance to talk not the class. > So you need to use critter.talk() And the talk > method does not take any arguments except > self. What you are doing here is calling the > class method with an instance value of farmlet. > ie self in that method gets the value of farmlet. > > > def main(): > >crit1 = Critter("Sweetie") > >crit2 = Critter("Dave") > >farmlet = [crit1,crit2] > >f = Farm(farmlet) > > > >choice = None > >while choice != "0": > >print \ > >(""" > >Critter Caretaker > > > >0 - Quit > >1 - Listen to your critter > >2 - Feed your critter > >3 - Play with your critter > >""") > > > >choice = input("Choice: ") > >print() > > > ># exit > >if choice == "0": > >print("Good-bye.") > > > ># listen to your critter > >elif choice == "1": > >f.talk(farmlet) > > > ># feed your critter > >elif choice == "2": > >f.eat(farmlet) > > Note that f.eat is a method you inherit from Critter. > The Critter method does not take any arguments > so this will fail. > > ># play with your critter > >elif choice == "3": > >f.play(farmlet) > > Same with f.play() > > > Traceback (most recent call last): > > File "D:/David/Python/programs/critter_farm3.py", line 72, in talk > >Critter.talk(farmlet) > > File "D:/David/Python/programs/critter_farm3.py", line 38, in talk > >print("I'm", self.name, "and I feel", self.mood, "now.\n") > > AttributeError: 'list' object has no attribute 'name' > > This is because you are accessing the method via > the class and passing farmlet as the instance value > rather than sending the message to the innstance > directly. Use > > critter.talk() # and no farmlet needed! > > HTH, > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Class methods
Can someone show me how to code this correctly please? # Critter Caretaker # A virtual pet to care for class Critter(object): """A virtual pet""" def __init__(self, name, hunger = 0, boredom = 0): self.name = name self.hunger = hunger self.boredom = boredom # __ denotes private method def __pass_time(self): self.hunger += 1 self.boredom += 1 self.__str__() def __str__(self): print("Hunger is",self.hunger, "Boredom is " ,self.boredom) print("Unhappines is ",self.hunger + self.boredom," and Mood is ",self.mood) @property def mood(self): unhappiness = self.hunger + self.boredom if unhappiness < 5: m = "happy" elif 5 <= unhappiness <= 10: m = "okay" elif 11 <= unhappiness <= 15: m = "frustrated" else: m = "mad" return m def talk(self): for critter in farmlet: print("I'm", self.name, "and I feel", self.mood, "now.\n") self.__pass_time() def eat(self): food = int(input("Enter how much food you want to feed your critter: ")) print("Brruppp. Thank you.") self.hunger -= food # hunger = 0 at iniatition # self.hunger = self.boredom - food if self.hunger < 0: self.hunger = 0 self.__pass_time() def play(self): fun = int(input("Enter how much fun you want your critter to have: ")) print("Wheee!") self.boredom -= fun # boredom = 0 at iniatition # self.boredom = self.boredom - fun if self.boredom < 0: self.boredom = 0 self.__pass_time() ##class Farm(Critter): ##def __init__(self,farmlet): ## Critter.__init__(self,farmlet) ## self.farmlet = farmlet ## ##def talk(self,farmlet): ##for critter in farmlet: ##print("Hello") ##Critter.talk(farmlet) def main(): crit1 = Critter("Sweetie") crit2 = Critter("Dave") farmlet = [crit1,crit2] choice = None while choice != "0": print \ (""" Critter Caretaker 0 - Quit 1 - Listen to your critter 2 - Feed your critter 3 - Play with your critter """) choice = input("Choice: ") print() # exit if choice == "0": print("Good-bye.") # listen to your critter elif choice == "1": for critter in farmlet: farmlet.talk() # feed your critter elif choice == "2": farmlet.eat() # play with your critter elif choice == "3": f.play(farmlet) # some unknown choice else: print("\nSorry, but", choice, "isn't a valid choice.") main() ("\n\nPress the enter key to exit.") *Output* Critter Caretaker 0 - Quit 1 - Listen to your critter 2 - Feed your critter 3 - Play with your critter Choice: 1 Traceback (most recent call last): File "I:/Python/programs/critter_farm4.py", line 117, in main() File "I:/Python/programs/critter_farm4.py", line 103, in main farmlet.talk() AttributeError: 'list' object has no attribute 'talk' -- Dave Merrick merrick...@gmail.com Ph 03 3423 121 Cell 027 3089 169 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class methods
David, 2011/6/22 David Merrick : > # listen to your critter > elif choice == "1": > for critter in farmlet: > farmlet.talk() You want to call .talk() on your "critter" instance which has the .talk() method, not on farmlet (which is a list as the error message states) > Traceback (most recent call last): > File "I:/Python/programs/critter_farm4.py", line 117, in > main() > File "I:/Python/programs/critter_farm4.py", line 103, in main > farmlet.talk() > AttributeError: 'list' object has no attribute 'talk' You will probably have a problem when calling talk() because it references to the "farmlet" variable which doesn't exist in the scope of your .talk() method. HTH, -- Alex | twitter.com/alexconrad ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class methods
Just to add a little to Alexandre's answer. You can keep most of the code the same just add in farmlet[0].eat() farmlet[1].eat() and it will be okay... kinda. Or you could rewrite it and do it another way... I'm guessing that you are using python 3 by your print statements, so I don't think you need the int() around your input, even in python.2x input() was safe for numbers I believe (the whole list will rip my throat out if I'm wrong anyways, lol), but in your while loop around line 82 you have all the conditions responding to a string, even thought you explicitly changed the input to an integer. if choice == "0" instead of if choice == 0: so I got a continual output of ('\nSorry, but', 3, "isn't a valid choice.") Now this does not get your program running the way you want it to, but it will start you off making the corrections you need. Just some things to think about, in line 113, when did you define class f? in line 102, what are you trying to do, and are you telling it the right thing? since you have 2 (or possibly more) critters you are taking care of, how does the player know which one he is feeding / talking to / playing with in your farmlet? Anyways best of luck in your program, sounds pretty cool... What is it about you... that intrigues me so? From: Alexandre Conrad To: David Merrick Cc: tutor@python.org Sent: Wednesday, June 22, 2011 6:48 PM Subject: Re: [Tutor] Class methods David, 2011/6/22 David Merrick : > # listen to your critter > elif choice == "1": > for critter in farmlet: > farmlet.talk() You want to call .talk() on your "critter" instance which has the .talk() method, not on farmlet (which is a list as the error message states) > Traceback (most recent call last): > File "I:/Python/programs/critter_farm4.py", line 117, in > main() > File "I:/Python/programs/critter_farm4.py", line 103, in main > farmlet.talk() > AttributeError: 'list' object has no attribute 'talk' You will probably have a problem when calling talk() because it references to the "farmlet" variable which doesn't exist in the scope of your .talk() method. HTH, -- Alex | twitter.com/alexconrad ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class methods
"michael scott" wrote you are using python 3 by your print statements, so I don't think you need the int() around your input, Yes he does because in Python 3 input is the same as raw_input in Python 2 even in python.2x input() was safe for numbers I believe (the whole list will rip my throat out if I'm wrong anyways rip, rip, rip. :-) In Python 2 input could be used for numbers but it was not "safe", which is why input was effectively removed in Python 3 and raw_input renamed to input. In Python 2 input() evaluated whatever was typed as a Python expression which made it very unsafe. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class methods
"David Merrick" wrote Can someone show me how to code this correctly please? We've been doing that but you are still making some very basic mistakes which reflect a deep misunderastanding of what you are doing. You really should take several steps back and review the use of variables and functions and then reread the introductory material on classes and objects. Until you get the basics right you will struggle and even if somebody fixes all the issues in this code, the minute you try another program all the same problems will bite you. That having been said I'll make some comments: class Critter(object): """A virtual pet""" def __init__(self, name, hunger = 0, boredom = 0): self.name = name self.hunger = hunger self.boredom = boredom This is OK. # __ denotes private method def __pass_time(self): self.hunger += 1 self.boredom += 1 self.__str__() The last line does nothing useful. (It prints stuff just now but that's because of a conceptual fault in your str method. What you should really be saying here is: print (self) def __str__(self): print("Hunger is",self.hunger, "Boredom is " ,self.boredom) print("Unhappines is ",self.hunger + self.boredom," and Mood is ",self.mood) __str__ methods are supposed to return a string which can be used (or printed) by other code. They should not print anything themselves. This should do something like: return """Hunger is %s, Boredom is %s Unhappines is %s and Mood is%s""" % ( self.hunger, self.boredom, self.hunger+self.boredom, self.mood) @property def mood(self): unhappiness = self.hunger + self.boredom if unhappiness < 5: m = "happy" elif 5 <= unhappiness <= 10: m = "okay" elif 11 <= unhappiness <= 15: m = "frustrated" else: m = "mad" return m OK def talk(self): for critter in farmlet: print("I'm", self.name, "and I feel", self.mood, "now.\n") self.__pass_time() Problem: What is farmlet here? There is no farmlet in the method. There is no self.farmlet you can access. So now your class is tied to the external farmlet variable in the global scope, making your class pretty much useless in any other context. Also it will print the same message as many times as there are critters in farmlet - 2 in this case. But the message won't change because self still only applies to the current object. You are iterating over the critters in your main() function, you don't need to do it inside the critter itself. Each critter should only comment on its own state. I'd also prefer that this returned a string too because putting print statements inside methods limits the usefulness of the class. How would it work in a GUI version for example? Better to return a string and print that where you call it. def eat(self): food = int(input("Enter how much food you want to feed your critter: ")) print("Brruppp. Thank you.") self.hunger -= food # hunger = 0 at iniatition # self.hunger = self.boredom - food if self.hunger < 0: self.hunger = 0 self.__pass_time() OK, I think def play(self): fun = int(input("Enter how much fun you want your critter to have: ")) print("Wheee!") self.boredom -= fun # boredom = 0 at iniatition # self.boredom = self.boredom - fun if self.boredom < 0: self.boredom = 0 self.__pass_time() OK, I think def main(): crit1 = Critter("Sweetie") crit2 = Critter("Dave") farmlet = [crit1,crit2] OK so far, we have a list with two Critters choice = None while choice != "0": print \ (""" Critter Caretaker 0 - Quit 1 - Listen to your critter 2 - Feed your critter 3 - Play with your critter """) choice = input("Choice: ") print() Also OK we now have a choice and a loop. if choice == "0": print("Good-bye.") And we exit so the program eventually stops, which is good. elif choice == "1": for critter in farmlet: farmlet.talk() But here we are asking the list to do stuiff which is not good. It is the critters that talk. So you need to send talk() to the critter not the list: for critter in farmlet: critter.talk() # feed your critter elif choice == "2": farmlet.eat() Again lists can't eat, you need to feed weither an individual crittter - get the user to choose which? - or feed all your critters using the same code pattern as for talk above. # play with your critter elif choice == "3": f.play(farmlet) You don't have an f and you don't now ha
[Tutor] Using os.path.walk
I have a bunch of files I need to remove in dirs and subdirs when they are older than 7 days. I was looking at os.path.walk, to recurse the directories, but I'm having some difficulties with getting it to return the directory names or find examples of it's use for something like this. So far, I am trying to get it to just list the files in the directories and subdirs as such: def file_list(a, dir, files): print (dir): os.path.walk('/etc', dir_list, None) Ultimately, I want it to find to stat mtime and just list, then remove the files and directories, older than the seven days, but I can't seem to get past just returning a list of files correctly. Are there some clear examples that someone can point me to? Becky Here is a partial output of what I'm returning: /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/e /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/t /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/c / /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/m /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/a /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/i /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/l / /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/D /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/e /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/f /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/a /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/u /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/l /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/t /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/M /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/e /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/s /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/s /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/a /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/g /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/e /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/s /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/. /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/b /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/u /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/n /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/d /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/l /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/e / /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/C /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/o /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/n /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/t /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/e /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/n /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/t /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/s / /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/R /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/e /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/s /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/o /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/u /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/r /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/c /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/e /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/s / /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/F /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/r /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/e /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/n /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/c /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/h /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/. /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/l /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/p /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/r /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/o /etc/mail/DefaultMessages.bundle/Contents/Resources/French.lproj/j /etc/mail/DefaultMessages.bundle/Contents/Resources/German.lproj / /etc/mail/DefaultMessages.bundle/Contents/Resources/German.lproj/e /etc/mail/DefaultMessages.bundle/Contents/Resources/German.lproj/t /etc/mail/DefaultMessages.bundle/Contents/Resources/German.lproj/c / /etc/mail/DefaultMessages.bundle/Contents/Resources/G
Re: [Tutor] Using os.path.walk
On Thu, Jun 23, 2011 at 9:02 AM, Becky Mcquilling wrote: > I have a bunch of files I need to remove in dirs and subdirs when they are > older than 7 days. > I was looking at os.path.walk, to recurse the directories, but I'm having > some difficulties with getting it to return the directory names or find > examples of it's use for something like this. > So far, I am trying to get it to just list the files in the directories and > subdirs as such: > def file_list(a, dir, files): > print (dir): > os.path.walk('/etc', dir_list, None) > Ultimately, I want it to find to stat mtime and just list, then remove the > files and directories, older than the seven days, but I can't seem to get > past just returning a list of files correctly. > Are there some clear examples that someone can point me to? > Becky os.path.walk is deprecated in favour of os.walk. There are a couple of examples[1] in the documentation. The second example shows how to remove everything under a directory. You should be able to modify it to remove files that satisfy your criteria. The os.path.getmtime function[2] gets the mtime of a file. [1] http://docs.python.org/library/os.html#os.walk [2] http://docs.python.org/library/os.path.html#os.path.getmtime -- regards, kushal ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor