create a text file

Ben Finney bignose+hates-spam at benfinney.id.au
Wed May 31 07:29:12 EDT 2006


"per9000" <per9000 at gmail.com> writes:

> appendix = '.txt' # or '.csv' or whatever
> 
> #  some code
> 
> c += 1
> filetitle = 'result_' + zeropadding(c) + str(c)
> myfile = open(filetitle+appendix,'w')

Resist the urge to construct strings by concatenation, when Python's
built-in string formatting operations will do the job more elegantly.

    >>> filesuffix = ".txt"
    >>> count = 123
    >>> filename = "result_%05d%s" % (count, filesuffix)
    >>> print filename
    result_00123.txt

-- 
 \        "When you go in for a job interview, I think a good thing to |
  `\               ask is if they ever press charges."  -- Jack Handey |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list