Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!multikabel.net!newsfeed20.multikabel.net!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'matches': 0.07; 'subject:file': 0.07; 'filename': 0.09; 'filenames': 0.09; 'subject:create': 0.09; 'subject:number': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'be:': 0.16; 'expression,': 0.16; 'imports': 0.16; 'numbered': 0.16; 'range,': 0.16; 'range.': 0.16; 'specifying': 0.16; 'substring': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '>': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'url:mailman': 0.30; '-0700,': 0.31; 'file': 0.32; 'regular': 0.32; 'url:python': 0.33; 'subject:with': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'done,': 0.36; 'sequence': 0.36; 'url:listinfo': 0.36; 'next': 0.36; 'thanks': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'list': 0.37; 'subject:new': 0.38; 'pm,': 0.38; 'highest': 0.39; 'url:mail': 0.40; 'skip:n 30': 0.60; 'new': 0.61; 'email addr:gmail.com': 0.63; 'to:addr:gmail.com': 0.65; 'skip:n 40': 0.81; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=0xJB2mptFxNeKkVqjDERNqhFHS7ktU7rZznm79v4dQo=; b=yH/NzAEYo9H+vmCGGTJ/n2SScHUnNCidNtwfnes/ugGs2nbYKycRZhUThhqPlQwYNB Bfejh6nldp17CN0HJ+o60R6kl7SJ66zbWDVY/VnitQMoZSxCSRZcxPZJCkZZ6fqpL+hf J995Wk/JT42gTtvvT8rPiJKUrG8mZ6Y/9mTgfA5NQKMwP2X4Urra21vzSqh5xxtGTbnN 6JAml7AW1I5OKZH7risaGJ9h5+seFPpc9KAsaAO+MV08iaHxCDKPQpWNu+q1q4CE2oUE CvpjrabiPN8hH4TwZbH89g8/cG/xIGmB/1B9vSmQAHkKX81Ss7+2hioYCjzDa582noFY mmjA== MIME-Version: 1.0 X-Received: by 10.50.1.11 with SMTP id 11mr4659884igi.39.1369652427013; Mon, 27 May 2013 04:00:27 -0700 (PDT) In-Reply-To: References: <3c99daa0-6f30-4cbf-b107-56021a324f7c@googlegroups.com> Date: Mon, 27 May 2013 16:30:26 +0530 Subject: Re: How to create new python file with increament number, if doesn't exist? From: Avnesh Shakya To: Denis McMahon Content-Type: multipart/alternative; boundary=e89a8f83a2436112f004ddb10e13 Cc: python-list@python.org 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: 93 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369652430 news.xs4all.nl 15982 [2001:888:2000:d::a6]:40389 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46188 --e89a8f83a2436112f004ddb10e13 Content-Type: text/plain; charset=ISO-8859-1 Thanks On Mon, May 27, 2013 at 4:19 PM, Denis McMahon wrote: > On Mon, 27 May 2013 02:27:59 -0700, Avnesh Shakya wrote: > > > I want to create a new python file like 'data0.0.5', but if it is > > already exist then it should create 'data0.0.6', if it's also exist > > then next like 'data0.0.7'. I have done, but with range, please give > > me suggestion so that I can do it with specifying range. > > Try and put your description into the sequence of instructions you want > the computer follow. > > For this problem, my sequence of instructions would be: > > 1) Find the highest numbered existing file that matches the filename > data0.0.[number] > > 2) Create a new file that is one number higher. > > Now the solution is easy. Find the list of filenames in the directory > that match a suitable regular expression, take the numeric value of a > substring of the filename for each file and find the highest, add one to > it, then create the new file name. > > Something like the following (untested) with the relevant imports etc: > > nfn="data0.0."+str(max([int(f[8:])for f in os.listdir(p)if re.match > ('^data0.0.[0-9]+$',f)])+1) > > -- > Denis McMahon, denismfmcmahon@gmail.com > -- > http://mail.python.org/mailman/listinfo/python-list > --e89a8f83a2436112f004ddb10e13 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Thanks


On Mon, May 27, 2013 at 4:19 PM, Denis McMahon <denismfmcmahon@gmail.com> wrote:
On Mon, 27 May 2013 02:27:= 59 -0700, Avnesh Shakya wrote:

> I want to create a new python file like 'data0.0.5', but if it= is
> already exist then it should create 'data0.0.6', if it's a= lso exist
> then next like 'data0.0.7'. I have done, but with range, pleas= e give
> me suggestion so that I can do it with specifying range.

Try and put your description into the sequence of instructions you wa= nt
the computer follow.

For this problem, my sequence of instructions would be:

1) Find the highest numbered existing file that matches the filename
data0.0.[number]

2) Create a new file that is one number higher.

Now the solution is easy. Find the list of filenames in the directory
that match a suitable regular expression, take the numeric value of a
substring of the filename for each file and find the highest, add one to it, then create the new file name.

Something like the following (untested) with the relevant imports etc:

nfn=3D"data0.0."+str(max([int(f[8:])for f in os.listdir(p)if re.m= atch
('^data0.0.[0-9]+$',f)])+1)

--
Denis McMahon, denismfmcmahon@g= mail.com
--
http://mail.python.org/mailman/listinfo/python-list

--e89a8f83a2436112f004ddb10e13--