find files that have specific word in a loop?
up vote
1
down vote
favorite
In that folder there are files called: a11.shp,a11.shx,u21.shp,u21.shx
import os
words = ('a11','u21')
for root, dirs,files in os.walk(r'C:UsersuserDesktopfolder1'):
for i in files:
if i in words:
print(i)
TypeError: tuple indices must be integers or slices, not str
It requires some kind of an index for the tuple to run for all the items.
I want to use it as 'contains' that word and it doesn't work like contains the in in this case.
How can this be done when giving these exact words in the tuple. Without startswith.
python
add a comment |
up vote
1
down vote
favorite
In that folder there are files called: a11.shp,a11.shx,u21.shp,u21.shx
import os
words = ('a11','u21')
for root, dirs,files in os.walk(r'C:UsersuserDesktopfolder1'):
for i in files:
if i in words:
print(i)
TypeError: tuple indices must be integers or slices, not str
It requires some kind of an index for the tuple to run for all the items.
I want to use it as 'contains' that word and it doesn't work like contains the in in this case.
How can this be done when giving these exact words in the tuple. Without startswith.
python
do you want to match the exact words, or just partial words? For instance cana11_xxxx.txtmatchesa11?
– Jean-François Fabre
Nov 22 at 14:50
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
In that folder there are files called: a11.shp,a11.shx,u21.shp,u21.shx
import os
words = ('a11','u21')
for root, dirs,files in os.walk(r'C:UsersuserDesktopfolder1'):
for i in files:
if i in words:
print(i)
TypeError: tuple indices must be integers or slices, not str
It requires some kind of an index for the tuple to run for all the items.
I want to use it as 'contains' that word and it doesn't work like contains the in in this case.
How can this be done when giving these exact words in the tuple. Without startswith.
python
In that folder there are files called: a11.shp,a11.shx,u21.shp,u21.shx
import os
words = ('a11','u21')
for root, dirs,files in os.walk(r'C:UsersuserDesktopfolder1'):
for i in files:
if i in words:
print(i)
TypeError: tuple indices must be integers or slices, not str
It requires some kind of an index for the tuple to run for all the items.
I want to use it as 'contains' that word and it doesn't work like contains the in in this case.
How can this be done when giving these exact words in the tuple. Without startswith.
python
python
edited Nov 22 at 14:44
asked Nov 22 at 14:35
user10680652
234
234
do you want to match the exact words, or just partial words? For instance cana11_xxxx.txtmatchesa11?
– Jean-François Fabre
Nov 22 at 14:50
add a comment |
do you want to match the exact words, or just partial words? For instance cana11_xxxx.txtmatchesa11?
– Jean-François Fabre
Nov 22 at 14:50
do you want to match the exact words, or just partial words? For instance can
a11_xxxx.txt matches a11 ?– Jean-François Fabre
Nov 22 at 14:50
do you want to match the exact words, or just partial words? For instance can
a11_xxxx.txt matches a11 ?– Jean-François Fabre
Nov 22 at 14:50
add a comment |
2 Answers
2
active
oldest
votes
up vote
-1
down vote
accepted
I think the (working) idea of if words in i: would be if any(w in i for w in words), but that matches substrings (not exact strings) and is not very fast.
You have to do this the other way round. Test file_without_extension in words.
For instance like this:
for i in files:
# check if the filename (without extension) is in "words" iterable
if os.path.splitext(i)[0] in words:
print(i)
and if you have a lot of "words" make that a set instead of a tuple for faster lookup (words = {'a11','u21'}).
Can you write an example of what you describe?
– user10680652
Nov 22 at 14:42
I did. Just added your code around now
– Jean-François Fabre
Nov 22 at 14:43
1
I'm more inclined to agree withif any(w in i for w in words)sincefilename in wordswould require a full match. It may be that OP is only looking for partial match (although the example shows a full one). Also might explain why they shy away fromstartswith.
– Idlehands
Nov 22 at 14:48
OP requests: "How can this be done when giving these exact words in the tuple. Without startswith" I may have understood it wrong.
– Jean-François Fabre
Nov 22 at 14:49
yes, that's what I thought using any. Nice, but I wrote it wrong. Or thisos.path.basename(i).split('.')[0] in words.
– user10680652
Nov 22 at 14:53
|
show 1 more comment
up vote
0
down vote
I find that choosing descriptive names for variables makes the code easier to read. Examine my example below:
import os
words = ('a11','u21')
for root, dirs, files in os.walk(r'C:UsersuserDesktopfolder1'):
for filename in files:
for word in words:
if word in filename:
print('Match: "', word, '" is in "', file, '"', sep='')
This code produces the result I think you are aiming for.
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',
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%2f53433228%2ffind-files-that-have-specific-word-in-a-loop%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
up vote
-1
down vote
accepted
I think the (working) idea of if words in i: would be if any(w in i for w in words), but that matches substrings (not exact strings) and is not very fast.
You have to do this the other way round. Test file_without_extension in words.
For instance like this:
for i in files:
# check if the filename (without extension) is in "words" iterable
if os.path.splitext(i)[0] in words:
print(i)
and if you have a lot of "words" make that a set instead of a tuple for faster lookup (words = {'a11','u21'}).
Can you write an example of what you describe?
– user10680652
Nov 22 at 14:42
I did. Just added your code around now
– Jean-François Fabre
Nov 22 at 14:43
1
I'm more inclined to agree withif any(w in i for w in words)sincefilename in wordswould require a full match. It may be that OP is only looking for partial match (although the example shows a full one). Also might explain why they shy away fromstartswith.
– Idlehands
Nov 22 at 14:48
OP requests: "How can this be done when giving these exact words in the tuple. Without startswith" I may have understood it wrong.
– Jean-François Fabre
Nov 22 at 14:49
yes, that's what I thought using any. Nice, but I wrote it wrong. Or thisos.path.basename(i).split('.')[0] in words.
– user10680652
Nov 22 at 14:53
|
show 1 more comment
up vote
-1
down vote
accepted
I think the (working) idea of if words in i: would be if any(w in i for w in words), but that matches substrings (not exact strings) and is not very fast.
You have to do this the other way round. Test file_without_extension in words.
For instance like this:
for i in files:
# check if the filename (without extension) is in "words" iterable
if os.path.splitext(i)[0] in words:
print(i)
and if you have a lot of "words" make that a set instead of a tuple for faster lookup (words = {'a11','u21'}).
Can you write an example of what you describe?
– user10680652
Nov 22 at 14:42
I did. Just added your code around now
– Jean-François Fabre
Nov 22 at 14:43
1
I'm more inclined to agree withif any(w in i for w in words)sincefilename in wordswould require a full match. It may be that OP is only looking for partial match (although the example shows a full one). Also might explain why they shy away fromstartswith.
– Idlehands
Nov 22 at 14:48
OP requests: "How can this be done when giving these exact words in the tuple. Without startswith" I may have understood it wrong.
– Jean-François Fabre
Nov 22 at 14:49
yes, that's what I thought using any. Nice, but I wrote it wrong. Or thisos.path.basename(i).split('.')[0] in words.
– user10680652
Nov 22 at 14:53
|
show 1 more comment
up vote
-1
down vote
accepted
up vote
-1
down vote
accepted
I think the (working) idea of if words in i: would be if any(w in i for w in words), but that matches substrings (not exact strings) and is not very fast.
You have to do this the other way round. Test file_without_extension in words.
For instance like this:
for i in files:
# check if the filename (without extension) is in "words" iterable
if os.path.splitext(i)[0] in words:
print(i)
and if you have a lot of "words" make that a set instead of a tuple for faster lookup (words = {'a11','u21'}).
I think the (working) idea of if words in i: would be if any(w in i for w in words), but that matches substrings (not exact strings) and is not very fast.
You have to do this the other way round. Test file_without_extension in words.
For instance like this:
for i in files:
# check if the filename (without extension) is in "words" iterable
if os.path.splitext(i)[0] in words:
print(i)
and if you have a lot of "words" make that a set instead of a tuple for faster lookup (words = {'a11','u21'}).
edited Nov 22 at 14:43
answered Nov 22 at 14:40
Jean-François Fabre
99.8k953109
99.8k953109
Can you write an example of what you describe?
– user10680652
Nov 22 at 14:42
I did. Just added your code around now
– Jean-François Fabre
Nov 22 at 14:43
1
I'm more inclined to agree withif any(w in i for w in words)sincefilename in wordswould require a full match. It may be that OP is only looking for partial match (although the example shows a full one). Also might explain why they shy away fromstartswith.
– Idlehands
Nov 22 at 14:48
OP requests: "How can this be done when giving these exact words in the tuple. Without startswith" I may have understood it wrong.
– Jean-François Fabre
Nov 22 at 14:49
yes, that's what I thought using any. Nice, but I wrote it wrong. Or thisos.path.basename(i).split('.')[0] in words.
– user10680652
Nov 22 at 14:53
|
show 1 more comment
Can you write an example of what you describe?
– user10680652
Nov 22 at 14:42
I did. Just added your code around now
– Jean-François Fabre
Nov 22 at 14:43
1
I'm more inclined to agree withif any(w in i for w in words)sincefilename in wordswould require a full match. It may be that OP is only looking for partial match (although the example shows a full one). Also might explain why they shy away fromstartswith.
– Idlehands
Nov 22 at 14:48
OP requests: "How can this be done when giving these exact words in the tuple. Without startswith" I may have understood it wrong.
– Jean-François Fabre
Nov 22 at 14:49
yes, that's what I thought using any. Nice, but I wrote it wrong. Or thisos.path.basename(i).split('.')[0] in words.
– user10680652
Nov 22 at 14:53
Can you write an example of what you describe?
– user10680652
Nov 22 at 14:42
Can you write an example of what you describe?
– user10680652
Nov 22 at 14:42
I did. Just added your code around now
– Jean-François Fabre
Nov 22 at 14:43
I did. Just added your code around now
– Jean-François Fabre
Nov 22 at 14:43
1
1
I'm more inclined to agree with
if any(w in i for w in words) since filename in words would require a full match. It may be that OP is only looking for partial match (although the example shows a full one). Also might explain why they shy away from startswith.– Idlehands
Nov 22 at 14:48
I'm more inclined to agree with
if any(w in i for w in words) since filename in words would require a full match. It may be that OP is only looking for partial match (although the example shows a full one). Also might explain why they shy away from startswith.– Idlehands
Nov 22 at 14:48
OP requests: "How can this be done when giving these exact words in the tuple. Without startswith" I may have understood it wrong.
– Jean-François Fabre
Nov 22 at 14:49
OP requests: "How can this be done when giving these exact words in the tuple. Without startswith" I may have understood it wrong.
– Jean-François Fabre
Nov 22 at 14:49
yes, that's what I thought using any. Nice, but I wrote it wrong. Or this
os.path.basename(i).split('.')[0] in words.– user10680652
Nov 22 at 14:53
yes, that's what I thought using any. Nice, but I wrote it wrong. Or this
os.path.basename(i).split('.')[0] in words.– user10680652
Nov 22 at 14:53
|
show 1 more comment
up vote
0
down vote
I find that choosing descriptive names for variables makes the code easier to read. Examine my example below:
import os
words = ('a11','u21')
for root, dirs, files in os.walk(r'C:UsersuserDesktopfolder1'):
for filename in files:
for word in words:
if word in filename:
print('Match: "', word, '" is in "', file, '"', sep='')
This code produces the result I think you are aiming for.
add a comment |
up vote
0
down vote
I find that choosing descriptive names for variables makes the code easier to read. Examine my example below:
import os
words = ('a11','u21')
for root, dirs, files in os.walk(r'C:UsersuserDesktopfolder1'):
for filename in files:
for word in words:
if word in filename:
print('Match: "', word, '" is in "', file, '"', sep='')
This code produces the result I think you are aiming for.
add a comment |
up vote
0
down vote
up vote
0
down vote
I find that choosing descriptive names for variables makes the code easier to read. Examine my example below:
import os
words = ('a11','u21')
for root, dirs, files in os.walk(r'C:UsersuserDesktopfolder1'):
for filename in files:
for word in words:
if word in filename:
print('Match: "', word, '" is in "', file, '"', sep='')
This code produces the result I think you are aiming for.
I find that choosing descriptive names for variables makes the code easier to read. Examine my example below:
import os
words = ('a11','u21')
for root, dirs, files in os.walk(r'C:UsersuserDesktopfolder1'):
for filename in files:
for word in words:
if word in filename:
print('Match: "', word, '" is in "', file, '"', sep='')
This code produces the result I think you are aiming for.
answered Nov 22 at 14:54
figbeam
2,580137
2,580137
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53433228%2ffind-files-that-have-specific-word-in-a-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
do you want to match the exact words, or just partial words? For instance can
a11_xxxx.txtmatchesa11?– Jean-François Fabre
Nov 22 at 14:50