Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; 'messages.': 0.05; 'tree': 0.05; 'binary': 0.07; 'none:': 0.07; 'reject': 0.07; 'complicate': 0.09; 'pep': 0.09; 'subject:files': 0.09; 'try:': 0.09; 'def': 0.12; '"from': 0.16; '"is': 0.16; "'''": 0.16; "'from": 0.16; "('new',": 0.16; '>to': 0.16; 'caches': 0.16; 'else"': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'ioerror:': 0.16; 'maildir': 0.16; 'matters,': 0.16; 'message- id:@cskk.homeip.net': 0.16; 'os.walk': 0.16; 'points:': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'script,': 0.16; 'simpson': 0.16; 'skip:> 20': 0.16; 'subdir': 0.16; 'subject:between': 0.16; 'text",': 0.16; 'url:peps': 0.16; 'language': 0.16; 'wrote:': 0.18; 'header:User- Agent:1': 0.23; 'directory.': 0.24; 'url:dev': 0.24; 'fine': 0.24; 'cheers,': 0.24; 'this:': 0.26; 'subject:/': 0.26; 'header:In- Reply-To:1': 0.27; 'points': 0.29; 'possibility': 0.29; 'reporting': 0.29; 'tim': 0.29; 'code': 0.31; "skip:' 10": 0.31; 'chase': 0.31; 'flags': 0.31; 'them?': 0.31; 'file': 0.32; 'open': 0.33; 'url:python': 0.33; 'beginning': 0.33; 'except': 0.35; 'knows': 0.35; 'something': 0.35; 'no,': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'c++': 0.36; 'false': 0.36; 'received:com.au': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'email addr:python.org': 0.37; 'being': 0.38; 'sometimes': 0.38; 'received:211': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'little': 0.38; 'structure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'tell': 0.60; 'skip:o 30': 0.61; 'first': 0.61; 'content-disposition:inline': 0.62; "you've": 0.63; 'such': 0.63; 'myself': 0.63; 'more': 0.64; 'love': 0.65; 'email name:python-list': 0.65; 'holding': 0.65; 'here': 0.66; 'six': 0.68; 'walk': 0.74; 'special': 0.74; 'directories,': 0.84; 'gabriel': 0.84 Date: Mon, 1 Sep 2014 12:07:51 +1000 From: Cameron Simpson To: python-list@python.org Subject: Re: Distinguishing between maildir, mbox, and MH files/directories? MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: quoted-printable In-Reply-To: <20140831134525.25a4321e@bigbox.christie.dr> User-Agent: Mutt/1.5.21 (2010-09-15) References: <20140831134525.25a4321e@bigbox.christie.dr> X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.1 cv=fvDlOjIf c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=IelBwl_1Nt8A:10 a=zAuMQy6v8ngA:10 a=IkcTkHD0fZMA:10 a=vrnE16BAAAAA:8 a=8uR6FKqcAAAA:8 a=8AHkEIZyAAAA:8 a=LlobtveMp2ZiSaavzWMA:9 a=QEXdDO2ut3YA:10 a=VhVvL8HfBcoA:10 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409537281 news.xs4all.nl 2863 [2001:888:2000:d::a6]:47177 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77381 On 31Aug2014 13:45, Tim Chase wrote: >Tinkering around with a little script, I found myself with the need >to walk a directory tree and process mail messaged found within. >Sometimes these end up being mbox files (with multiple messages >within), sometimes it's a Maildir structure with messages in each >individual file and extra holding directories, and sometimes it's a >MH directory. To complicate matters, there's also the possibility of >non-{mbox,maildir,mh) files such as binary MUA caches appearing >alongside these messages. > >Python knows how to handle each just fine as long as I tell it what >type of file to expect. But is there a straight-forward way to >distinguish them? (FWIW, the *nix "file" utility is just reporting >"ASCII text", sometimes "with very long lines", and sometimes >erroneously flags them as C or C++ files=E2=80=BD). > >All I need is "is it maildir, mbox, mh, or something else" (I don't >have to get more complex for the "something else") inside an os.walk >loop. Here is my code for these tests: def ismhdir(path): ''' Test if `path` points at an MH directory. ''' return os.path.isfile(os.path.join(path, '.mh_sequences')) def ismaildir(path): ''' Test if `path` points at a Maildir directory. ''' for subdir in ('new', 'cur', 'tmp'): if not os.path.isdir(os.path.join(path,subdir)): return False return True def ismbox(path): ''' Open path and check that its first line begins with "From ". ''' fp=3DNone try: fp=3Dopen(path) from_ =3D fp.read(5) except IOError: if fp is not None: fp.close() return False fp.close() return from_ =3D=3D 'From ' I would use these is code somewhat like this (imagining your use case): if ismaildir(path): ... elif ismhdir(path): ... elif ismbox(path): ... else: reject other known special files here continue traversing downward otherwise Cheers, Cameron Simpson Gabriel Genellina: See PEP 234 http://www.python.org/dev/peps/pep-0234/ Angus Rodgers: You've got to love a language whose documentation contains sentences beginning like this: "Among its chief virtues are the following four -- no, five -- no, six -- points: [...]" =66rom python-list@python.org