Python 3.6 - enumerate files
I am trying to loop a series of jpg files in a folder. I found example code of that:
for n, image_file in enumerate(os.scandir(image_folder)):
which will loop through the image files in image_folder
. However, it seems like it is not following any sequence. I have my files name like 000001.jpg
, 000002.jpg
, 000003.jpg
,... and so on. But when the code run, it did not follow the sequence:
000213.jpg
000012.jpg
000672.jpg
....
What seems to be the issue here?
python python-3.x
add a comment |
I am trying to loop a series of jpg files in a folder. I found example code of that:
for n, image_file in enumerate(os.scandir(image_folder)):
which will loop through the image files in image_folder
. However, it seems like it is not following any sequence. I have my files name like 000001.jpg
, 000002.jpg
, 000003.jpg
,... and so on. But when the code run, it did not follow the sequence:
000213.jpg
000012.jpg
000672.jpg
....
What seems to be the issue here?
python python-3.x
3
Have you read the documentation? If you did, you would know the answer. As for the remedy, have the files sorted before enumerating them.
– DYZ
Jan 3 at 16:01
... and, because you wantfile_9.jpg
to come beforefile_10.jpg
, have a look at the natsort module
– gboffi
Jan 3 at 16:24
add a comment |
I am trying to loop a series of jpg files in a folder. I found example code of that:
for n, image_file in enumerate(os.scandir(image_folder)):
which will loop through the image files in image_folder
. However, it seems like it is not following any sequence. I have my files name like 000001.jpg
, 000002.jpg
, 000003.jpg
,... and so on. But when the code run, it did not follow the sequence:
000213.jpg
000012.jpg
000672.jpg
....
What seems to be the issue here?
python python-3.x
I am trying to loop a series of jpg files in a folder. I found example code of that:
for n, image_file in enumerate(os.scandir(image_folder)):
which will loop through the image files in image_folder
. However, it seems like it is not following any sequence. I have my files name like 000001.jpg
, 000002.jpg
, 000003.jpg
,... and so on. But when the code run, it did not follow the sequence:
000213.jpg
000012.jpg
000672.jpg
....
What seems to be the issue here?
python python-3.x
python python-3.x
asked Jan 3 at 15:57
sooonsooon
1,69633369
1,69633369
3
Have you read the documentation? If you did, you would know the answer. As for the remedy, have the files sorted before enumerating them.
– DYZ
Jan 3 at 16:01
... and, because you wantfile_9.jpg
to come beforefile_10.jpg
, have a look at the natsort module
– gboffi
Jan 3 at 16:24
add a comment |
3
Have you read the documentation? If you did, you would know the answer. As for the remedy, have the files sorted before enumerating them.
– DYZ
Jan 3 at 16:01
... and, because you wantfile_9.jpg
to come beforefile_10.jpg
, have a look at the natsort module
– gboffi
Jan 3 at 16:24
3
3
Have you read the documentation? If you did, you would know the answer. As for the remedy, have the files sorted before enumerating them.
– DYZ
Jan 3 at 16:01
Have you read the documentation? If you did, you would know the answer. As for the remedy, have the files sorted before enumerating them.
– DYZ
Jan 3 at 16:01
... and, because you want
file_9.jpg
to come before file_10.jpg
, have a look at the natsort module– gboffi
Jan 3 at 16:24
... and, because you want
file_9.jpg
to come before file_10.jpg
, have a look at the natsort module– gboffi
Jan 3 at 16:24
add a comment |
2 Answers
2
active
oldest
votes
Here's the relevant bit on os.scandir():
os.scandir(path='.')
Return an iterator of
os.DirEntry
objects
corresponding to the entries in the directory given by path. The
entries are yielded in arbitrary order, and the special entries'.'
and'..'
are not included.
You should not expect it to be in any particular order. The same goes for listdir()
if you were considering this as an alternative.
If you strictly need them to be in order, consider sorting them first:
scanned = sorted([f for f in os.scandir(image_folder)], key=lambda f: f.name)
for n, image_file in enumerate(scanned):
# ... rest of your code
I addedscanned = sorted([f for f in os.scandir(image_folder)])
and getting error:TypeError: '<' not supported between instances of 'posix.DirEntry' and 'posix.DirEntry'
– sooon
Jan 3 at 16:13
Try my edit with custom sorted key.
– Idlehands
Jan 3 at 16:16
add a comment |
I prefer to use glob
:
The glob module finds all the pathnames matching a specified pattern
according to the rules used by the Unix shell, although results are
returned in arbitrary order. No tilde expansion is done, but *, ?, and
character ranges expressed with will be correctly matched.
You will need this if you handle more complex file structures so starting with glob isnt that bad. For your case you also can use os.scandir()
as mentioned above.
Reference: glob module
import glob
files = sorted(glob.glob(r"C:UsersFabianDesktopstackimg*.jpg"))
for key, myfile in enumerate(files):
print(key, myfile)
notice even if there other files like .txt
they wont be in your list
Output:
C:UsersFabianDesktopstack>python c:/Users/Fabian/Desktop/stack/img.py
0 C:UsersFabianDesktopstackimgimg0001.jpg
1 C:UsersFabianDesktopstackimgimg0002.jpg
2 C:UsersFabianDesktopstackimgimg0003.jpg
....
add a comment |
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%2f54025747%2fpython-3-6-enumerate-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's the relevant bit on os.scandir():
os.scandir(path='.')
Return an iterator of
os.DirEntry
objects
corresponding to the entries in the directory given by path. The
entries are yielded in arbitrary order, and the special entries'.'
and'..'
are not included.
You should not expect it to be in any particular order. The same goes for listdir()
if you were considering this as an alternative.
If you strictly need them to be in order, consider sorting them first:
scanned = sorted([f for f in os.scandir(image_folder)], key=lambda f: f.name)
for n, image_file in enumerate(scanned):
# ... rest of your code
I addedscanned = sorted([f for f in os.scandir(image_folder)])
and getting error:TypeError: '<' not supported between instances of 'posix.DirEntry' and 'posix.DirEntry'
– sooon
Jan 3 at 16:13
Try my edit with custom sorted key.
– Idlehands
Jan 3 at 16:16
add a comment |
Here's the relevant bit on os.scandir():
os.scandir(path='.')
Return an iterator of
os.DirEntry
objects
corresponding to the entries in the directory given by path. The
entries are yielded in arbitrary order, and the special entries'.'
and'..'
are not included.
You should not expect it to be in any particular order. The same goes for listdir()
if you were considering this as an alternative.
If you strictly need them to be in order, consider sorting them first:
scanned = sorted([f for f in os.scandir(image_folder)], key=lambda f: f.name)
for n, image_file in enumerate(scanned):
# ... rest of your code
I addedscanned = sorted([f for f in os.scandir(image_folder)])
and getting error:TypeError: '<' not supported between instances of 'posix.DirEntry' and 'posix.DirEntry'
– sooon
Jan 3 at 16:13
Try my edit with custom sorted key.
– Idlehands
Jan 3 at 16:16
add a comment |
Here's the relevant bit on os.scandir():
os.scandir(path='.')
Return an iterator of
os.DirEntry
objects
corresponding to the entries in the directory given by path. The
entries are yielded in arbitrary order, and the special entries'.'
and'..'
are not included.
You should not expect it to be in any particular order. The same goes for listdir()
if you were considering this as an alternative.
If you strictly need them to be in order, consider sorting them first:
scanned = sorted([f for f in os.scandir(image_folder)], key=lambda f: f.name)
for n, image_file in enumerate(scanned):
# ... rest of your code
Here's the relevant bit on os.scandir():
os.scandir(path='.')
Return an iterator of
os.DirEntry
objects
corresponding to the entries in the directory given by path. The
entries are yielded in arbitrary order, and the special entries'.'
and'..'
are not included.
You should not expect it to be in any particular order. The same goes for listdir()
if you were considering this as an alternative.
If you strictly need them to be in order, consider sorting them first:
scanned = sorted([f for f in os.scandir(image_folder)], key=lambda f: f.name)
for n, image_file in enumerate(scanned):
# ... rest of your code
edited Jan 3 at 16:13
answered Jan 3 at 16:01
IdlehandsIdlehands
6,1631923
6,1631923
I addedscanned = sorted([f for f in os.scandir(image_folder)])
and getting error:TypeError: '<' not supported between instances of 'posix.DirEntry' and 'posix.DirEntry'
– sooon
Jan 3 at 16:13
Try my edit with custom sorted key.
– Idlehands
Jan 3 at 16:16
add a comment |
I addedscanned = sorted([f for f in os.scandir(image_folder)])
and getting error:TypeError: '<' not supported between instances of 'posix.DirEntry' and 'posix.DirEntry'
– sooon
Jan 3 at 16:13
Try my edit with custom sorted key.
– Idlehands
Jan 3 at 16:16
I added
scanned = sorted([f for f in os.scandir(image_folder)])
and getting error:TypeError: '<' not supported between instances of 'posix.DirEntry' and 'posix.DirEntry'
– sooon
Jan 3 at 16:13
I added
scanned = sorted([f for f in os.scandir(image_folder)])
and getting error:TypeError: '<' not supported between instances of 'posix.DirEntry' and 'posix.DirEntry'
– sooon
Jan 3 at 16:13
Try my edit with custom sorted key.
– Idlehands
Jan 3 at 16:16
Try my edit with custom sorted key.
– Idlehands
Jan 3 at 16:16
add a comment |
I prefer to use glob
:
The glob module finds all the pathnames matching a specified pattern
according to the rules used by the Unix shell, although results are
returned in arbitrary order. No tilde expansion is done, but *, ?, and
character ranges expressed with will be correctly matched.
You will need this if you handle more complex file structures so starting with glob isnt that bad. For your case you also can use os.scandir()
as mentioned above.
Reference: glob module
import glob
files = sorted(glob.glob(r"C:UsersFabianDesktopstackimg*.jpg"))
for key, myfile in enumerate(files):
print(key, myfile)
notice even if there other files like .txt
they wont be in your list
Output:
C:UsersFabianDesktopstack>python c:/Users/Fabian/Desktop/stack/img.py
0 C:UsersFabianDesktopstackimgimg0001.jpg
1 C:UsersFabianDesktopstackimgimg0002.jpg
2 C:UsersFabianDesktopstackimgimg0003.jpg
....
add a comment |
I prefer to use glob
:
The glob module finds all the pathnames matching a specified pattern
according to the rules used by the Unix shell, although results are
returned in arbitrary order. No tilde expansion is done, but *, ?, and
character ranges expressed with will be correctly matched.
You will need this if you handle more complex file structures so starting with glob isnt that bad. For your case you also can use os.scandir()
as mentioned above.
Reference: glob module
import glob
files = sorted(glob.glob(r"C:UsersFabianDesktopstackimg*.jpg"))
for key, myfile in enumerate(files):
print(key, myfile)
notice even if there other files like .txt
they wont be in your list
Output:
C:UsersFabianDesktopstack>python c:/Users/Fabian/Desktop/stack/img.py
0 C:UsersFabianDesktopstackimgimg0001.jpg
1 C:UsersFabianDesktopstackimgimg0002.jpg
2 C:UsersFabianDesktopstackimgimg0003.jpg
....
add a comment |
I prefer to use glob
:
The glob module finds all the pathnames matching a specified pattern
according to the rules used by the Unix shell, although results are
returned in arbitrary order. No tilde expansion is done, but *, ?, and
character ranges expressed with will be correctly matched.
You will need this if you handle more complex file structures so starting with glob isnt that bad. For your case you also can use os.scandir()
as mentioned above.
Reference: glob module
import glob
files = sorted(glob.glob(r"C:UsersFabianDesktopstackimg*.jpg"))
for key, myfile in enumerate(files):
print(key, myfile)
notice even if there other files like .txt
they wont be in your list
Output:
C:UsersFabianDesktopstack>python c:/Users/Fabian/Desktop/stack/img.py
0 C:UsersFabianDesktopstackimgimg0001.jpg
1 C:UsersFabianDesktopstackimgimg0002.jpg
2 C:UsersFabianDesktopstackimgimg0003.jpg
....
I prefer to use glob
:
The glob module finds all the pathnames matching a specified pattern
according to the rules used by the Unix shell, although results are
returned in arbitrary order. No tilde expansion is done, but *, ?, and
character ranges expressed with will be correctly matched.
You will need this if you handle more complex file structures so starting with glob isnt that bad. For your case you also can use os.scandir()
as mentioned above.
Reference: glob module
import glob
files = sorted(glob.glob(r"C:UsersFabianDesktopstackimg*.jpg"))
for key, myfile in enumerate(files):
print(key, myfile)
notice even if there other files like .txt
they wont be in your list
Output:
C:UsersFabianDesktopstack>python c:/Users/Fabian/Desktop/stack/img.py
0 C:UsersFabianDesktopstackimgimg0001.jpg
1 C:UsersFabianDesktopstackimgimg0002.jpg
2 C:UsersFabianDesktopstackimgimg0003.jpg
....
answered Jan 3 at 16:26
FabianFabian
18211
18211
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%2f54025747%2fpython-3-6-enumerate-files%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
3
Have you read the documentation? If you did, you would know the answer. As for the remedy, have the files sorted before enumerating them.
– DYZ
Jan 3 at 16:01
... and, because you want
file_9.jpg
to come beforefile_10.jpg
, have a look at the natsort module– gboffi
Jan 3 at 16:24