Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #46184 > unrolled thread
| Started by | Avnesh Shakya <avnesh.nitk@gmail.com> |
|---|---|
| First post | 2013-05-27 02:27 -0700 |
| Last post | 2013-05-27 11:13 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
How to create new python file with increament number, if doesn't exist? Avnesh Shakya <avnesh.nitk@gmail.com> - 2013-05-27 02:27 -0700
Re: How to create new python file with increament number, if doesn't exist? Denis McMahon <denismfmcmahon@gmail.com> - 2013-05-27 10:49 +0000
Re: How to create new python file with increament number, if doesn't exist? Avnesh Shakya <avnesh.nitk@gmail.com> - 2013-05-27 16:30 +0530
Re: How to create new python file with increament number, if doesn't exist? jt@toerring.de (Jens Thoms Toerring) - 2013-05-27 11:13 +0000
| From | Avnesh Shakya <avnesh.nitk@gmail.com> |
|---|---|
| Date | 2013-05-27 02:27 -0700 |
| Subject | How to create new python file with increament number, if doesn't exist? |
| Message-ID | <3c99daa0-6f30-4cbf-b107-56021a324f7c@googlegroups.com> |
hi,
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.
I was trying this way and it's working also..
i = 0
for i in range(100):
try:
with open('Data%d.%d.%d.json'%(0,0,i,)): pass
continue
except IOError:
edxCorrectDataFile = file('Data%d.%d.%d.json'%(0,0,i,), 'a+')
break
But here I have defined range 100, Is it possible without range it create many required files?
Thanks
[toc] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-05-27 10:49 +0000 |
| Message-ID | <knvdms$gvn$3@dont-email.me> |
| In reply to | #46184 |
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
[toc] | [prev] | [next] | [standalone]
| From | Avnesh Shakya <avnesh.nitk@gmail.com> |
|---|---|
| Date | 2013-05-27 16:30 +0530 |
| Message-ID | <mailman.2245.1369652430.3114.python-list@python.org> |
| In reply to | #46186 |
[Multipart message — attachments visible in raw view] — view raw
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 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
>
[toc] | [prev] | [next] | [standalone]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Date | 2013-05-27 11:13 +0000 |
| Message-ID | <b0gtefF8i5vU1@mid.uni-berlin.de> |
| In reply to | #46184 |
Avnesh Shakya <avnesh.nitk@gmail.com> 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.
> I was trying this way and it's working also..
> i = 0
> for i in range(100):
> try:
> with open('Data%d.%d.%d.json'%(0,0,i,)): pass
> continue
> except IOError:
> edxCorrectDataFile = file('Data%d.%d.%d.json'%(0,0,i,), 'a+')
> break
> But here I have defined range 100, Is it possible without range it create
> many required files?
What about something as simple as this?
i = 0
while os.path.exists( 'Data{0}.{1}.{2}.json'.format( 0, 0, i ) ) :
i += 1
f = open( 'Data{0}.{1}.{2}.json'.format( 0, 0, i ), 'w' )
For your code you'ld make it e.g.
i = 0
try:
while True :
with open('Data%d.%d.%d.json'%(0,0,i)):
i += 1
except IOError:
edxCorrectDataFile = file('Data%d.%d.%d.json'%(0,0,i), 'a+')
Note that I don't see how all this trying to open a file and
catching an exeception if it doesn't exist is any better
than simply using os.path.exists(). You may have read some-
where that it avoids a race condition and would thus be more
secure. That's not the case here, an attacker still could
create e.g. a symbolic link with the name of the file you're
going to open between the time the exception is thrown and your
program getting around to open the file you expect not to exist,
so nothing is gained by using this somewhat convoluted method.
What would be needed for avoiding a race condition is an addi-
tional flag to be passed to open() like the for example the
O_EXCL flag that can be passed to Unix' open() system function.
But that isn't supported by Pythons open() function which
rather likeky is based on C's fopen() function). The best
advice to avoid such problems is probably not to open files
with a predictable name (or better to use one of the methods
to create files with names guaranteed to be unique by the
system) in directories to which other users than you also
have write permission.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web