Thanks so much!! >----Messaggio originale---- >Da: tutor-requ...@python.org >Data: 23/07/2014 8.10 >A: <tutor@python.org> >Ogg: Tutor Digest, Vol 125, Issue 71 > >Send Tutor mailing list submissions to > tutor@python.org > >To subscribe or unsubscribe via the World Wide Web, visit > https://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. How to show dictionary item non present on file > (jarod...@libero.it) > 2. Re: How to show dictionary item non present on file > (Steven D'Aprano) > 3. Re: How to show dictionary item non present on file (Peter Otten) > 4. Getting a directory listing with Python to MySQL (Eric Dannewitz) > 5. Re: Getting a directory listing with Python to MySQL (Danny Yoo) > 6. Re: Getting a directory listing with Python to MySQL > (Steven D'Aprano) > 7. Re: Getting a directory listing with Python to MySQL > (Eric Dannewitz) > > >---------------------------------------------------------------------- > >Message: 1 >Date: Tue, 22 Jul 2014 13:10:18 +0200 (CEST) >From: "jarod...@libero.it" <jarod...@libero.it> >To: tutor@python.org >Subject: [Tutor] How to show dictionary item non present on file >Message-ID: > <253805423.250161406027418296.JavaMail.defaultUser@defaultHost> >Content-Type: text/plain;charset="UTF-8" > >Hin there!!! > >I have a niave question on dictionary analysis: >If you have a dictionary like this: >diz >Out[8]: {'elenour': 1, 'frank': 1, 'jack': 1, 'ralph': 1} > >and you have a list and you want to know which keys are not present on my >dictionary the code are simple. >for i in diz.keys(): > ...: if i in mitico: > ...: print "NO" > ...: else: > ...: print i > ...: >NO > >But I havethis problem I have a file and I want to know which elements are not >present on my file from dictionary. > more data.tmp >jack 1 >pippo 1 >luis 1 >frate 1 >livio 1 >frank 1 > > >with open("data.tmp") as p: > for i in p: > lines= i.strip("\n").split("\t") > if not diz.has_key(lines[0]): > ....: print i > ....: >pippo 1 > >luis 1 > >frate 1 > >livio 1 > >The output I want is to have : >ralph and 'elenour.. how can I do this? >thanks in advance! > > > >------------------------------ > >Message: 2 >Date: Tue, 22 Jul 2014 21:32:47 +1000 >From: Steven D'Aprano <st...@pearwood.info> >To: tutor@python.org >Subject: Re: [Tutor] How to show dictionary item non present on file >Message-ID: <20140722113247.GO9112@ando> >Content-Type: text/plain; charset=us-ascii > >On Tue, Jul 22, 2014 at 01:10:18PM +0200, jarod...@libero.it wrote: > >> But I havethis problem I have a file and I want to know which elements are not >> present on my file from dictionary. >> more data.tmp >> jack 1 >> pippo 1 >> luis 1 >> frate 1 >> livio 1 >> frank 1 >> >> >> with open("data.tmp") as p: >> for i in p: >> lines= i.strip("\n").split("\t") >> if not diz.has_key(lines[0]): >> ....: print i >> ....: >> pippo 1 >> luis 1 >> frate 1 >> livio 1 >> >> The output I want is to have : >> ralph and 'elenour.. how can I do this? > >You are doing the comparison the wrong way: you are saying: > >for each line in the file: > is the line in the dict? > if no, print the line > > >What you want is: > >for each key in the dict: > is the key in the file? > if no, print the key > > >It is not easy to try searching the file directly, so we copy the lines >from the file into a set: > >lines = set() >with open("data.tmp") as the_file: > for line in the_file: > line = line.strip().split("\t")[0] > lines.add(line) > > >Here is a shorter way to do the same thing: > >with open("data.tmp") as the_file: > lines = set([line.strip().split("\t")[0] for line in the_file]) > > >Now you can walk through the dict: > >for name in diz: > if name not in lines: > print(name) > > >Or, if you prefer: > >names = set(diz) # copy the keys from the dict into a set >print(names.difference(lines)) > > >If you want to see the other way around: > >print(lines.difference(names)) > > > > >-- >Steven > > >------------------------------ > >Message: 3 >Date: Tue, 22 Jul 2014 13:48:09 +0200 >From: Peter Otten <__pete...@web.de> >To: tutor@python.org >Subject: Re: [Tutor] How to show dictionary item non present on file >Message-ID: <lqlj1q$6ph$1...@ger.gmane.org> >Content-Type: text/plain; charset="ISO-8859-1" > >jarod...@libero.it wrote: > >> Hin there!!! >> >> I have a niave question on dictionary analysis: >> If you have a dictionary like this: >> diz >> Out[8]: {'elenour': 1, 'frank': 1, 'jack': 1, 'ralph': 1} >> >> and you have a list and you want to know which keys are not present on my >> dictionary the code are simple. >> for i in diz.keys(): >> ...: if i in mitico: >> ...: print "NO" >> ...: else: >> ...: print i >> ...: >> NO >> >> But I havethis problem I have a file and I want to know which elements are >> not present on my file from dictionary. >> more data.tmp >> jack 1 >> pippo 1 >> luis 1 >> frate 1 >> livio 1 >> frank 1 >> >> >> with open("data.tmp") as p: >> for i in p: >> lines= i.strip("\n").split("\t") >> if not diz.has_key(lines[0]): >> ....: print i >> ....: >> pippo 1 >> >> luis 1 >> >> frate 1 >> >> livio 1 >> >> The output I want is to have : >> ralph and 'elenour.. how can I do this? >> thanks in advance! > >You have the logic backwards. You have to iterate over the names in the dict >and look them up in the file: > >>>> diz = {'elenour': 1, 'frank': 1, 'jack': 1, 'ralph': 1} >>>> def find_name(name): >... with open("data.tmp") as f: >... for line in f: >... if name == line.split("\t")[0]: >... return True >... return False >... >>>> for name in diz: >... if not find_name(name): >... print name >... >ralph >elenour > >However, this is very inefficient as you have to read the file len(diz) >times. It is better to store the names in a data structure suited for fast >lookup first and then to use that instead of the file. Python's dict and set >types are such data structures, so as we don't care about an associated >value let's use a set: > >>>> with open("data.tmp") as f: >... names_in_file = {line.split("\t")[0] for line in f} >... >>>> for name in diz: >... if not name in names_in_file: >... print name >... >ralph >elenour > >Digging a bit deeper you'll find that you can get these names with set >arithmetic: > >>>> set(diz) - names_in_file >set(['ralph', 'elenour']) > >or even: > >>>> diz.viewkeys() - names_in_file >set(['elenour', 'ralph']) > > > > > >------------------------------ > >Message: 4 >Date: Tue, 22 Jul 2014 16:10:02 -0700 (PDT) >From: Eric Dannewitz <edannew...@rdschool.org> >To: tutor@python.org >Subject: [Tutor] Getting a directory listing with Python to MySQL >Message-ID: > <156945655.610335.1406070602162.javamail.zim...@rdschool.org> >Content-Type: text/plain; charset="utf-8" > >Hello list, I'm new. I've done a few things in Python, but this one is posing problems. > >What I want to do is be able to parse a directory, say /Volumes/Stuff/Files/, and all the directories that might be in there, and be able to pick out file name, size, date modified, etc, and send that to a MySQL database. Any ideas? Sounds like it should be easy but...... >-------------- next part -------------- >An HTML attachment was scrubbed... >URL: <http://mail.python. org/pipermail/tutor/attachments/20140722/0a8c664b/attachment-0001.html> > >------------------------------ > >Message: 5 >Date: Tue, 22 Jul 2014 18:14:55 -0700 >From: Danny Yoo <d...@hashcollision.org> >To: Eric Dannewitz <edannew...@rdschool.org> >Cc: Python Tutor Mailing List <tutor@python.org> >Subject: Re: [Tutor] Getting a directory listing with Python to MySQL >Message-ID: > <cagzapf7xi1ks5tk2c6e3x+o-e8nx9jnzq1kieucemqxayai...@mail.gmail.com> >Content-Type: text/plain; charset="utf-8" > >> What I want to do is be able to parse a directory, say >/Volumes/Stuff/Files/, and all the directories that might be in there, and >be able to pick out file name, size, date modified, etc, > >Hi Eric, > >You might find the following helpful: >http://www.diveintopython.net/file_handling/os_module.html >-------------- next part -------------- >An HTML attachment was scrubbed... >URL: <http://mail.python. org/pipermail/tutor/attachments/20140722/1011b814/attachment-0001.html> > >------------------------------ > >Message: 6 >Date: Wed, 23 Jul 2014 11:31:48 +1000 >From: Steven D'Aprano <st...@pearwood.info> >To: tutor@python.org >Subject: Re: [Tutor] Getting a directory listing with Python to MySQL >Message-ID: <20140723013146.GQ9112@ando> >Content-Type: text/plain; charset=us-ascii > >On Tue, Jul 22, 2014 at 04:10:02PM -0700, Eric Dannewitz wrote: >> Hello list, I'm new. I've done a few things in Python, but this one is posing problems. >> >> What I want to do is be able to parse a directory, say >> /Volumes/Stuff/Files/, and all the directories that might be in there, >> and be able to pick out file name, size, date modified, etc, and send >> that to a MySQL database. Any ideas? Sounds like it should be easy >> but...... > >os.listdir(path) returns all the entries under path (apart from '.' and >'..'). You can then test whether they are files, directories or links >with os.path.isdir, os.path.isfile, os.path.islink. (Remember that under >Linux and Unix, there things other than files and links that can live in >directories, e.g. named pipes.) > >But rather than manually iterate through the contents of the directory, >os.walk already does that for you. Something like this ought to get you >started: > >files = [] >for dirpath, dirnames, filenames in os.walk(start_path): > pathnames = [os.path.join(dirpath, name) for name in filenames] > files.extend(pathnames) > > >That gets you a list of all the files under start_path. To check their >size, dates, etc. use the os.stat and os.lstat functions (os.stat >follows symbolic links, os.lstat does not). The stat module has a bunch >of symbolic constants which may be helpful for interpreting the results. > > > > >-- >Steven > > >------------------------------ > >Message: 7 >Date: Tue, 22 Jul 2014 18:32:43 -0700 (PDT) >From: Eric Dannewitz <edannew...@rdschool.org> >To: Python Tutor Mailing List <tutor@python.org> >Subject: Re: [Tutor] Getting a directory listing with Python to MySQL >Message-ID: > <1073549999.618930.1406079163039.javamail.zim...@rdschool.org> >Content-Type: text/plain; charset="utf-8" > >That's close. I have been playing from glob and os.walk but I'm at a loss how to get the size, creation and modified date while running it. > >----- Original Message ----- > >From: "Danny Yoo" <d...@hashcollision.org> >To: "Eric Dannewitz" <edannew...@rdschool.org> >Cc: "Python Tutor Mailing List" <tutor@python.org> >Sent: Tuesday, July 22, 2014 6:14:55 PM >Subject: Re: [Tutor] Getting a directory listing with Python to MySQL > > > > >> What I want to do is be able to parse a directory, say /Volumes/Stuff/Files/, and all the directories that might be in there, and be able to pick out file name, size, date modified, etc, > >Hi Eric, > >You might find the following helpful: http://www.diveintopython. net/file_handling/os_module.html > >-------------- next part -------------- >An HTML attachment was scrubbed... >URL: <http://mail.python. org/pipermail/tutor/attachments/20140722/cbaaa832/attachment.html> > >------------------------------ > >Subject: Digest Footer > >_______________________________________________ >Tutor maillist - Tutor@python.org >https://mail.python.org/mailman/listinfo/tutor > > >------------------------------ > >End of Tutor Digest, Vol 125, Issue 71 >************************************** >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor