How to pythonically avoid this code duplication with a while loop?
I want to find the first filename myfile????.txt
that doesn't exist yet (????
is a number). This works:
import os
i = 0
f = 'myfile%04i.txt' % i
while os.path.exists(f):
i += 1
f = 'myfile%04i.txt' % i
but I don't like the code duplication of f = ...
.
Is there a pythonic way to avoid the code duplication in this while
loop?
NB: I have posted a half-satisfactory solution, using a do/while
idiom, as mentioned in the main answer of Emulate a do-while loop in Python?, but I still wonder if there is better way for this particular case (thus, it's not a dupe of this question).
python loops while-loop code-duplication
add a comment |
I want to find the first filename myfile????.txt
that doesn't exist yet (????
is a number). This works:
import os
i = 0
f = 'myfile%04i.txt' % i
while os.path.exists(f):
i += 1
f = 'myfile%04i.txt' % i
but I don't like the code duplication of f = ...
.
Is there a pythonic way to avoid the code duplication in this while
loop?
NB: I have posted a half-satisfactory solution, using a do/while
idiom, as mentioned in the main answer of Emulate a do-while loop in Python?, but I still wonder if there is better way for this particular case (thus, it's not a dupe of this question).
python loops while-loop code-duplication
1
glob.glob
with someregex
would probably be the best way to go.
– Ev. Kounis
Nov 27 '18 at 14:35
1
A nice solution is coming next year: python.org/dev/peps/pep-0572
– VPfB
Nov 27 '18 at 15:02
add a comment |
I want to find the first filename myfile????.txt
that doesn't exist yet (????
is a number). This works:
import os
i = 0
f = 'myfile%04i.txt' % i
while os.path.exists(f):
i += 1
f = 'myfile%04i.txt' % i
but I don't like the code duplication of f = ...
.
Is there a pythonic way to avoid the code duplication in this while
loop?
NB: I have posted a half-satisfactory solution, using a do/while
idiom, as mentioned in the main answer of Emulate a do-while loop in Python?, but I still wonder if there is better way for this particular case (thus, it's not a dupe of this question).
python loops while-loop code-duplication
I want to find the first filename myfile????.txt
that doesn't exist yet (????
is a number). This works:
import os
i = 0
f = 'myfile%04i.txt' % i
while os.path.exists(f):
i += 1
f = 'myfile%04i.txt' % i
but I don't like the code duplication of f = ...
.
Is there a pythonic way to avoid the code duplication in this while
loop?
NB: I have posted a half-satisfactory solution, using a do/while
idiom, as mentioned in the main answer of Emulate a do-while loop in Python?, but I still wonder if there is better way for this particular case (thus, it's not a dupe of this question).
python loops while-loop code-duplication
python loops while-loop code-duplication
edited Nov 27 '18 at 14:40
Basj
asked Nov 27 '18 at 14:32
BasjBasj
6,26632107233
6,26632107233
1
glob.glob
with someregex
would probably be the best way to go.
– Ev. Kounis
Nov 27 '18 at 14:35
1
A nice solution is coming next year: python.org/dev/peps/pep-0572
– VPfB
Nov 27 '18 at 15:02
add a comment |
1
glob.glob
with someregex
would probably be the best way to go.
– Ev. Kounis
Nov 27 '18 at 14:35
1
A nice solution is coming next year: python.org/dev/peps/pep-0572
– VPfB
Nov 27 '18 at 15:02
1
1
glob.glob
with some regex
would probably be the best way to go.– Ev. Kounis
Nov 27 '18 at 14:35
glob.glob
with some regex
would probably be the best way to go.– Ev. Kounis
Nov 27 '18 at 14:35
1
1
A nice solution is coming next year: python.org/dev/peps/pep-0572
– VPfB
Nov 27 '18 at 15:02
A nice solution is coming next year: python.org/dev/peps/pep-0572
– VPfB
Nov 27 '18 at 15:02
add a comment |
5 Answers
5
active
oldest
votes
You do not need to follow the while
paradiagm here, a nested generator expression with next()
works:
import os
from itertools import count
f = next(f for f in ('myfile%04i.txt' % i for i in count()) if not os.path.exists(f))
print(f)
Perfect solution (I just edited so that we can easily test it by copying/pasting).
– Basj
Nov 27 '18 at 14:46
@Basj ok, note I just edited it a bit to correct a mistake (of mine)
– Chris_Rands
Nov 27 '18 at 14:47
1
This is really pythonic, concise, and explicit! I thought it would be possible with a generator / next() but I didn't find how to do it, thanks a lot!
– Basj
Nov 27 '18 at 14:49
add a comment |
Get rid of the f
variable.
import os
i = 0
while os.path.exists('myfile%04i.txt' % i):
i += 1
Oh great! But I still needf
at the end, is there a way to get it?
– Basj
Nov 27 '18 at 14:36
Makef
a variable and "give" it towhile
.
– Ctrl S
Nov 27 '18 at 14:37
@Basj Yes, you could still initializef
before the loop, then usewhile os.path.exists(f % i)
– Bill the Lizard
Nov 27 '18 at 14:39
2
The second solution doesn't work, if youprint f
, you'll getmyfile%04i.txt
and notmyfile0002.txt
for example. So we would need to addf = f % i
at the end to really get the answer inf
. It's probably the best solution so far though!
– Basj
Nov 27 '18 at 14:43
Ah, you're right. My bad for not running it. I'm going to revert my edit, since that's essentially what you started with.
– Bill the Lizard
Nov 27 '18 at 14:47
add a comment |
I nearly found the answer while writing the end of the question. After a few modifications, it works:
import os
i = 0
while True:
f = 'myfile%04i.txt' % i
if not os.path.exists(f):
break
i += 1
print f
Still I wonder if there is a more pythonic way, maybe with an iterator, generator, next(...)
or anything like this.
add a comment |
Is this too simple?
import os
f = 'myfile0000.txt'
while os.path.exists(f):
i += 1
f = 'myfile%04i.txt' % i
It "works", like my question's code, and the solution I posted, but I wanted to know if there is a solution without duplication off = ...
.
– Basj
Nov 27 '18 at 14:41
add a comment |
You could do:
import os
from itertools import count
cursor = count()
it = iter((path for path in map(lambda x: 'myfile%04i.txt' % x, cursor) if not os.path.exists(path)))
first = next(it, None)
if first:
print(first)
Output
myfile0000.txt
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53501978%2fhow-to-pythonically-avoid-this-code-duplication-with-a-while-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You do not need to follow the while
paradiagm here, a nested generator expression with next()
works:
import os
from itertools import count
f = next(f for f in ('myfile%04i.txt' % i for i in count()) if not os.path.exists(f))
print(f)
Perfect solution (I just edited so that we can easily test it by copying/pasting).
– Basj
Nov 27 '18 at 14:46
@Basj ok, note I just edited it a bit to correct a mistake (of mine)
– Chris_Rands
Nov 27 '18 at 14:47
1
This is really pythonic, concise, and explicit! I thought it would be possible with a generator / next() but I didn't find how to do it, thanks a lot!
– Basj
Nov 27 '18 at 14:49
add a comment |
You do not need to follow the while
paradiagm here, a nested generator expression with next()
works:
import os
from itertools import count
f = next(f for f in ('myfile%04i.txt' % i for i in count()) if not os.path.exists(f))
print(f)
Perfect solution (I just edited so that we can easily test it by copying/pasting).
– Basj
Nov 27 '18 at 14:46
@Basj ok, note I just edited it a bit to correct a mistake (of mine)
– Chris_Rands
Nov 27 '18 at 14:47
1
This is really pythonic, concise, and explicit! I thought it would be possible with a generator / next() but I didn't find how to do it, thanks a lot!
– Basj
Nov 27 '18 at 14:49
add a comment |
You do not need to follow the while
paradiagm here, a nested generator expression with next()
works:
import os
from itertools import count
f = next(f for f in ('myfile%04i.txt' % i for i in count()) if not os.path.exists(f))
print(f)
You do not need to follow the while
paradiagm here, a nested generator expression with next()
works:
import os
from itertools import count
f = next(f for f in ('myfile%04i.txt' % i for i in count()) if not os.path.exists(f))
print(f)
edited Nov 27 '18 at 14:47
answered Nov 27 '18 at 14:44
Chris_RandsChris_Rands
16.8k54075
16.8k54075
Perfect solution (I just edited so that we can easily test it by copying/pasting).
– Basj
Nov 27 '18 at 14:46
@Basj ok, note I just edited it a bit to correct a mistake (of mine)
– Chris_Rands
Nov 27 '18 at 14:47
1
This is really pythonic, concise, and explicit! I thought it would be possible with a generator / next() but I didn't find how to do it, thanks a lot!
– Basj
Nov 27 '18 at 14:49
add a comment |
Perfect solution (I just edited so that we can easily test it by copying/pasting).
– Basj
Nov 27 '18 at 14:46
@Basj ok, note I just edited it a bit to correct a mistake (of mine)
– Chris_Rands
Nov 27 '18 at 14:47
1
This is really pythonic, concise, and explicit! I thought it would be possible with a generator / next() but I didn't find how to do it, thanks a lot!
– Basj
Nov 27 '18 at 14:49
Perfect solution (I just edited so that we can easily test it by copying/pasting).
– Basj
Nov 27 '18 at 14:46
Perfect solution (I just edited so that we can easily test it by copying/pasting).
– Basj
Nov 27 '18 at 14:46
@Basj ok, note I just edited it a bit to correct a mistake (of mine)
– Chris_Rands
Nov 27 '18 at 14:47
@Basj ok, note I just edited it a bit to correct a mistake (of mine)
– Chris_Rands
Nov 27 '18 at 14:47
1
1
This is really pythonic, concise, and explicit! I thought it would be possible with a generator / next() but I didn't find how to do it, thanks a lot!
– Basj
Nov 27 '18 at 14:49
This is really pythonic, concise, and explicit! I thought it would be possible with a generator / next() but I didn't find how to do it, thanks a lot!
– Basj
Nov 27 '18 at 14:49
add a comment |
Get rid of the f
variable.
import os
i = 0
while os.path.exists('myfile%04i.txt' % i):
i += 1
Oh great! But I still needf
at the end, is there a way to get it?
– Basj
Nov 27 '18 at 14:36
Makef
a variable and "give" it towhile
.
– Ctrl S
Nov 27 '18 at 14:37
@Basj Yes, you could still initializef
before the loop, then usewhile os.path.exists(f % i)
– Bill the Lizard
Nov 27 '18 at 14:39
2
The second solution doesn't work, if youprint f
, you'll getmyfile%04i.txt
and notmyfile0002.txt
for example. So we would need to addf = f % i
at the end to really get the answer inf
. It's probably the best solution so far though!
– Basj
Nov 27 '18 at 14:43
Ah, you're right. My bad for not running it. I'm going to revert my edit, since that's essentially what you started with.
– Bill the Lizard
Nov 27 '18 at 14:47
add a comment |
Get rid of the f
variable.
import os
i = 0
while os.path.exists('myfile%04i.txt' % i):
i += 1
Oh great! But I still needf
at the end, is there a way to get it?
– Basj
Nov 27 '18 at 14:36
Makef
a variable and "give" it towhile
.
– Ctrl S
Nov 27 '18 at 14:37
@Basj Yes, you could still initializef
before the loop, then usewhile os.path.exists(f % i)
– Bill the Lizard
Nov 27 '18 at 14:39
2
The second solution doesn't work, if youprint f
, you'll getmyfile%04i.txt
and notmyfile0002.txt
for example. So we would need to addf = f % i
at the end to really get the answer inf
. It's probably the best solution so far though!
– Basj
Nov 27 '18 at 14:43
Ah, you're right. My bad for not running it. I'm going to revert my edit, since that's essentially what you started with.
– Bill the Lizard
Nov 27 '18 at 14:47
add a comment |
Get rid of the f
variable.
import os
i = 0
while os.path.exists('myfile%04i.txt' % i):
i += 1
Get rid of the f
variable.
import os
i = 0
while os.path.exists('myfile%04i.txt' % i):
i += 1
edited Nov 27 '18 at 14:47
answered Nov 27 '18 at 14:35
Bill the LizardBill the Lizard
294k157498789
294k157498789
Oh great! But I still needf
at the end, is there a way to get it?
– Basj
Nov 27 '18 at 14:36
Makef
a variable and "give" it towhile
.
– Ctrl S
Nov 27 '18 at 14:37
@Basj Yes, you could still initializef
before the loop, then usewhile os.path.exists(f % i)
– Bill the Lizard
Nov 27 '18 at 14:39
2
The second solution doesn't work, if youprint f
, you'll getmyfile%04i.txt
and notmyfile0002.txt
for example. So we would need to addf = f % i
at the end to really get the answer inf
. It's probably the best solution so far though!
– Basj
Nov 27 '18 at 14:43
Ah, you're right. My bad for not running it. I'm going to revert my edit, since that's essentially what you started with.
– Bill the Lizard
Nov 27 '18 at 14:47
add a comment |
Oh great! But I still needf
at the end, is there a way to get it?
– Basj
Nov 27 '18 at 14:36
Makef
a variable and "give" it towhile
.
– Ctrl S
Nov 27 '18 at 14:37
@Basj Yes, you could still initializef
before the loop, then usewhile os.path.exists(f % i)
– Bill the Lizard
Nov 27 '18 at 14:39
2
The second solution doesn't work, if youprint f
, you'll getmyfile%04i.txt
and notmyfile0002.txt
for example. So we would need to addf = f % i
at the end to really get the answer inf
. It's probably the best solution so far though!
– Basj
Nov 27 '18 at 14:43
Ah, you're right. My bad for not running it. I'm going to revert my edit, since that's essentially what you started with.
– Bill the Lizard
Nov 27 '18 at 14:47
Oh great! But I still need
f
at the end, is there a way to get it?– Basj
Nov 27 '18 at 14:36
Oh great! But I still need
f
at the end, is there a way to get it?– Basj
Nov 27 '18 at 14:36
Make
f
a variable and "give" it to while
.– Ctrl S
Nov 27 '18 at 14:37
Make
f
a variable and "give" it to while
.– Ctrl S
Nov 27 '18 at 14:37
@Basj Yes, you could still initialize
f
before the loop, then use while os.path.exists(f % i)
– Bill the Lizard
Nov 27 '18 at 14:39
@Basj Yes, you could still initialize
f
before the loop, then use while os.path.exists(f % i)
– Bill the Lizard
Nov 27 '18 at 14:39
2
2
The second solution doesn't work, if you
print f
, you'll get myfile%04i.txt
and not myfile0002.txt
for example. So we would need to add f = f % i
at the end to really get the answer in f
. It's probably the best solution so far though!– Basj
Nov 27 '18 at 14:43
The second solution doesn't work, if you
print f
, you'll get myfile%04i.txt
and not myfile0002.txt
for example. So we would need to add f = f % i
at the end to really get the answer in f
. It's probably the best solution so far though!– Basj
Nov 27 '18 at 14:43
Ah, you're right. My bad for not running it. I'm going to revert my edit, since that's essentially what you started with.
– Bill the Lizard
Nov 27 '18 at 14:47
Ah, you're right. My bad for not running it. I'm going to revert my edit, since that's essentially what you started with.
– Bill the Lizard
Nov 27 '18 at 14:47
add a comment |
I nearly found the answer while writing the end of the question. After a few modifications, it works:
import os
i = 0
while True:
f = 'myfile%04i.txt' % i
if not os.path.exists(f):
break
i += 1
print f
Still I wonder if there is a more pythonic way, maybe with an iterator, generator, next(...)
or anything like this.
add a comment |
I nearly found the answer while writing the end of the question. After a few modifications, it works:
import os
i = 0
while True:
f = 'myfile%04i.txt' % i
if not os.path.exists(f):
break
i += 1
print f
Still I wonder if there is a more pythonic way, maybe with an iterator, generator, next(...)
or anything like this.
add a comment |
I nearly found the answer while writing the end of the question. After a few modifications, it works:
import os
i = 0
while True:
f = 'myfile%04i.txt' % i
if not os.path.exists(f):
break
i += 1
print f
Still I wonder if there is a more pythonic way, maybe with an iterator, generator, next(...)
or anything like this.
I nearly found the answer while writing the end of the question. After a few modifications, it works:
import os
i = 0
while True:
f = 'myfile%04i.txt' % i
if not os.path.exists(f):
break
i += 1
print f
Still I wonder if there is a more pythonic way, maybe with an iterator, generator, next(...)
or anything like this.
answered Nov 27 '18 at 14:35
BasjBasj
6,26632107233
6,26632107233
add a comment |
add a comment |
Is this too simple?
import os
f = 'myfile0000.txt'
while os.path.exists(f):
i += 1
f = 'myfile%04i.txt' % i
It "works", like my question's code, and the solution I posted, but I wanted to know if there is a solution without duplication off = ...
.
– Basj
Nov 27 '18 at 14:41
add a comment |
Is this too simple?
import os
f = 'myfile0000.txt'
while os.path.exists(f):
i += 1
f = 'myfile%04i.txt' % i
It "works", like my question's code, and the solution I posted, but I wanted to know if there is a solution without duplication off = ...
.
– Basj
Nov 27 '18 at 14:41
add a comment |
Is this too simple?
import os
f = 'myfile0000.txt'
while os.path.exists(f):
i += 1
f = 'myfile%04i.txt' % i
Is this too simple?
import os
f = 'myfile0000.txt'
while os.path.exists(f):
i += 1
f = 'myfile%04i.txt' % i
answered Nov 27 '18 at 14:39
DominiqueDominique
1,96141841
1,96141841
It "works", like my question's code, and the solution I posted, but I wanted to know if there is a solution without duplication off = ...
.
– Basj
Nov 27 '18 at 14:41
add a comment |
It "works", like my question's code, and the solution I posted, but I wanted to know if there is a solution without duplication off = ...
.
– Basj
Nov 27 '18 at 14:41
It "works", like my question's code, and the solution I posted, but I wanted to know if there is a solution without duplication of
f = ...
.– Basj
Nov 27 '18 at 14:41
It "works", like my question's code, and the solution I posted, but I wanted to know if there is a solution without duplication of
f = ...
.– Basj
Nov 27 '18 at 14:41
add a comment |
You could do:
import os
from itertools import count
cursor = count()
it = iter((path for path in map(lambda x: 'myfile%04i.txt' % x, cursor) if not os.path.exists(path)))
first = next(it, None)
if first:
print(first)
Output
myfile0000.txt
add a comment |
You could do:
import os
from itertools import count
cursor = count()
it = iter((path for path in map(lambda x: 'myfile%04i.txt' % x, cursor) if not os.path.exists(path)))
first = next(it, None)
if first:
print(first)
Output
myfile0000.txt
add a comment |
You could do:
import os
from itertools import count
cursor = count()
it = iter((path for path in map(lambda x: 'myfile%04i.txt' % x, cursor) if not os.path.exists(path)))
first = next(it, None)
if first:
print(first)
Output
myfile0000.txt
You could do:
import os
from itertools import count
cursor = count()
it = iter((path for path in map(lambda x: 'myfile%04i.txt' % x, cursor) if not os.path.exists(path)))
first = next(it, None)
if first:
print(first)
Output
myfile0000.txt
answered Nov 27 '18 at 14:40
Daniel MesejoDaniel Mesejo
18.6k21432
18.6k21432
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53501978%2fhow-to-pythonically-avoid-this-code-duplication-with-a-while-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
glob.glob
with someregex
would probably be the best way to go.– Ev. Kounis
Nov 27 '18 at 14:35
1
A nice solution is coming next year: python.org/dev/peps/pep-0572
– VPfB
Nov 27 '18 at 15:02