Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '"""': 0.07; 'fname': 0.09; 'subject:create': 0.09; 'subject:files': 0.09; 'cc:addr:python- list': 0.11; 'python': 0.11; 'creates': 0.14; 'random': 0.14; 'template': 0.14; '"w")': 0.16; '(say': 0.16; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'integer.': 0.16; 'lowercase': 0.16; 'received:174.136': 0.16; 'sequential': 0.16; 'template,': 0.16; 'folder': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'possibly': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'words': 0.29; 'code': 0.31; 'bunch': 0.31; 'work:': 0.31; 'file': 0.32; 'charset :us-ascii': 0.36; 'too': 0.37; 'received:10': 0.37; 'skip:o 20': 0.38; 'files': 0.38; 'pm,': 0.38; 'skip:x 10': 0.40; 'most': 0.60; 'simple': 0.61; 'to:addr:gmail.com': 0.65; '20,': 0.68; 'million': 0.74; '2015': 0.84; 'stamp': 0.91; 'subject:Best': 0.91; 'dirty': 0.93 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1432134410894:378854121 X-MC-Ingress-Time: 1432134410894 Date: Wed, 20 May 2015 10:07:23 -0500 From: Tim Chase To: Chris Angelico Cc: "python-list@python.org" Subject: Re: Best approach to create humongous amount of files In-Reply-To: References: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AuthUser: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1432135585 news.xs4all.nl 2967 [2001:888:2000:d::a6]:41443 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90959 On 2015-05-20 22:58, Chris Angelico wrote: > On Wed, May 20, 2015 at 9:44 PM, Parul Mogra > wrote: > > My objective is to create large amount of data files (say a > > million *.json files), using a pre-existing template file > > (*.json). Each file would have a unique name, possibly by > > incorporating time stamp information. The files have to be > > generated in a folder specified. [snip] > try a simple sequential integer. > > All you'd need would be a loop that creates a bunch of files... most > of your code will be figuring out what parts of the template need to > change. Not too difficult. If you store your template as a Python string-formatting template, you can just use string-formatting to do your dirty work: import random HOW_MANY = 1000000 template = """{ "some_string": "%(string)s", "some_int": %(int)i } """ wordlist = [ word.rstrip() for word in open('/usr/share/dict/words') ] wordlist[:] = [ # just lowercase all-alpha words word for word in wordlist if word.isalpha() and word.islower() ] for i in xrange(HOW_MANY): fname = "data_%08i.json" % i with open(fname, "w") as f: f.write(template % { "string_value": random.choice(wordlist), "int_value": random.randint(0, 1000), }) -tkc