Recursively list files within a directory
$begingroup$
I have the following code to recursively list files within a directory:
from fnmatch import filter
from os import path, walk
def files_within(directory_path):
files_within =
for root, directory_path, files_path in walk(directory_path):
for file_path in filter(files_path, "*"):
files_within.append(path.join(root, file_path))
return files_within
I want change it to avoid side affects. Is it possible?
I'm not using the glob
because I'm using Python 2.5 because of a legacy code.
python python-2.x file-system
$endgroup$
add a comment |
$begingroup$
I have the following code to recursively list files within a directory:
from fnmatch import filter
from os import path, walk
def files_within(directory_path):
files_within =
for root, directory_path, files_path in walk(directory_path):
for file_path in filter(files_path, "*"):
files_within.append(path.join(root, file_path))
return files_within
I want change it to avoid side affects. Is it possible?
I'm not using the glob
because I'm using Python 2.5 because of a legacy code.
python python-2.x file-system
$endgroup$
add a comment |
$begingroup$
I have the following code to recursively list files within a directory:
from fnmatch import filter
from os import path, walk
def files_within(directory_path):
files_within =
for root, directory_path, files_path in walk(directory_path):
for file_path in filter(files_path, "*"):
files_within.append(path.join(root, file_path))
return files_within
I want change it to avoid side affects. Is it possible?
I'm not using the glob
because I'm using Python 2.5 because of a legacy code.
python python-2.x file-system
$endgroup$
I have the following code to recursively list files within a directory:
from fnmatch import filter
from os import path, walk
def files_within(directory_path):
files_within =
for root, directory_path, files_path in walk(directory_path):
for file_path in filter(files_path, "*"):
files_within.append(path.join(root, file_path))
return files_within
I want change it to avoid side affects. Is it possible?
I'm not using the glob
because I'm using Python 2.5 because of a legacy code.
python python-2.x file-system
python python-2.x file-system
edited 18 mins ago
Graipher
25k53687
25k53687
asked Jan 19 '17 at 12:09
FXuxFXux
5517
5517
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
Your function could be a generator (which were introduced in Python 2.2). It should also expose the pattern for the filter as a parameter:
import os
import fnmatch
def files_within(directory_path, pattern="*"):
for dirpath, dirnames, filenames in os.walk(directory_path):
for file_name in fnmatch.filter(filenames, pattern):
yield os.path.join(dirpath, file_name)
if __name__ == "__main__":
# You can simply iterate over the generator:
for file_path in files_within("."):
print file_path
# If you absolutely need a list:
file_list = list(files_within("../"))
# Use the pattern parameter:
text_files = list(files_within(".", "*.txt"))
I like to leave the names from os.walk
the same as in the documentation, but this is a personal choice.
What is not quite a personal choice is the number of tabs per indentation level. Python's official style-guide, PEP8, recommends using always 4 spaces.
I also changed the way of the imports. Here it already becomes obvious why importing them like this is better (as far as readability goes). It is obvious that fnmatch.filter
is from the module fnmatch
and not the built-in filter
.
$endgroup$
1
$begingroup$
@FátimaAlves You're welcome. Even though I'm still not entirely sure what you mean by "side effects" in your question. Was it the overriding of the function name with the internal local variable?
$endgroup$
– Graipher
Jan 19 '17 at 12:57
$begingroup$
I mean avoid reassign variables already assigned.
$endgroup$
– FXux
Jan 19 '17 at 13:09
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
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: "196"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fcodereview.stackexchange.com%2fquestions%2f153029%2frecursively-list-files-within-a-directory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Your function could be a generator (which were introduced in Python 2.2). It should also expose the pattern for the filter as a parameter:
import os
import fnmatch
def files_within(directory_path, pattern="*"):
for dirpath, dirnames, filenames in os.walk(directory_path):
for file_name in fnmatch.filter(filenames, pattern):
yield os.path.join(dirpath, file_name)
if __name__ == "__main__":
# You can simply iterate over the generator:
for file_path in files_within("."):
print file_path
# If you absolutely need a list:
file_list = list(files_within("../"))
# Use the pattern parameter:
text_files = list(files_within(".", "*.txt"))
I like to leave the names from os.walk
the same as in the documentation, but this is a personal choice.
What is not quite a personal choice is the number of tabs per indentation level. Python's official style-guide, PEP8, recommends using always 4 spaces.
I also changed the way of the imports. Here it already becomes obvious why importing them like this is better (as far as readability goes). It is obvious that fnmatch.filter
is from the module fnmatch
and not the built-in filter
.
$endgroup$
1
$begingroup$
@FátimaAlves You're welcome. Even though I'm still not entirely sure what you mean by "side effects" in your question. Was it the overriding of the function name with the internal local variable?
$endgroup$
– Graipher
Jan 19 '17 at 12:57
$begingroup$
I mean avoid reassign variables already assigned.
$endgroup$
– FXux
Jan 19 '17 at 13:09
add a comment |
$begingroup$
Your function could be a generator (which were introduced in Python 2.2). It should also expose the pattern for the filter as a parameter:
import os
import fnmatch
def files_within(directory_path, pattern="*"):
for dirpath, dirnames, filenames in os.walk(directory_path):
for file_name in fnmatch.filter(filenames, pattern):
yield os.path.join(dirpath, file_name)
if __name__ == "__main__":
# You can simply iterate over the generator:
for file_path in files_within("."):
print file_path
# If you absolutely need a list:
file_list = list(files_within("../"))
# Use the pattern parameter:
text_files = list(files_within(".", "*.txt"))
I like to leave the names from os.walk
the same as in the documentation, but this is a personal choice.
What is not quite a personal choice is the number of tabs per indentation level. Python's official style-guide, PEP8, recommends using always 4 spaces.
I also changed the way of the imports. Here it already becomes obvious why importing them like this is better (as far as readability goes). It is obvious that fnmatch.filter
is from the module fnmatch
and not the built-in filter
.
$endgroup$
1
$begingroup$
@FátimaAlves You're welcome. Even though I'm still not entirely sure what you mean by "side effects" in your question. Was it the overriding of the function name with the internal local variable?
$endgroup$
– Graipher
Jan 19 '17 at 12:57
$begingroup$
I mean avoid reassign variables already assigned.
$endgroup$
– FXux
Jan 19 '17 at 13:09
add a comment |
$begingroup$
Your function could be a generator (which were introduced in Python 2.2). It should also expose the pattern for the filter as a parameter:
import os
import fnmatch
def files_within(directory_path, pattern="*"):
for dirpath, dirnames, filenames in os.walk(directory_path):
for file_name in fnmatch.filter(filenames, pattern):
yield os.path.join(dirpath, file_name)
if __name__ == "__main__":
# You can simply iterate over the generator:
for file_path in files_within("."):
print file_path
# If you absolutely need a list:
file_list = list(files_within("../"))
# Use the pattern parameter:
text_files = list(files_within(".", "*.txt"))
I like to leave the names from os.walk
the same as in the documentation, but this is a personal choice.
What is not quite a personal choice is the number of tabs per indentation level. Python's official style-guide, PEP8, recommends using always 4 spaces.
I also changed the way of the imports. Here it already becomes obvious why importing them like this is better (as far as readability goes). It is obvious that fnmatch.filter
is from the module fnmatch
and not the built-in filter
.
$endgroup$
Your function could be a generator (which were introduced in Python 2.2). It should also expose the pattern for the filter as a parameter:
import os
import fnmatch
def files_within(directory_path, pattern="*"):
for dirpath, dirnames, filenames in os.walk(directory_path):
for file_name in fnmatch.filter(filenames, pattern):
yield os.path.join(dirpath, file_name)
if __name__ == "__main__":
# You can simply iterate over the generator:
for file_path in files_within("."):
print file_path
# If you absolutely need a list:
file_list = list(files_within("../"))
# Use the pattern parameter:
text_files = list(files_within(".", "*.txt"))
I like to leave the names from os.walk
the same as in the documentation, but this is a personal choice.
What is not quite a personal choice is the number of tabs per indentation level. Python's official style-guide, PEP8, recommends using always 4 spaces.
I also changed the way of the imports. Here it already becomes obvious why importing them like this is better (as far as readability goes). It is obvious that fnmatch.filter
is from the module fnmatch
and not the built-in filter
.
edited Jan 19 '17 at 12:43
answered Jan 19 '17 at 12:29
GraipherGraipher
25k53687
25k53687
1
$begingroup$
@FátimaAlves You're welcome. Even though I'm still not entirely sure what you mean by "side effects" in your question. Was it the overriding of the function name with the internal local variable?
$endgroup$
– Graipher
Jan 19 '17 at 12:57
$begingroup$
I mean avoid reassign variables already assigned.
$endgroup$
– FXux
Jan 19 '17 at 13:09
add a comment |
1
$begingroup$
@FátimaAlves You're welcome. Even though I'm still not entirely sure what you mean by "side effects" in your question. Was it the overriding of the function name with the internal local variable?
$endgroup$
– Graipher
Jan 19 '17 at 12:57
$begingroup$
I mean avoid reassign variables already assigned.
$endgroup$
– FXux
Jan 19 '17 at 13:09
1
1
$begingroup$
@FátimaAlves You're welcome. Even though I'm still not entirely sure what you mean by "side effects" in your question. Was it the overriding of the function name with the internal local variable?
$endgroup$
– Graipher
Jan 19 '17 at 12:57
$begingroup$
@FátimaAlves You're welcome. Even though I'm still not entirely sure what you mean by "side effects" in your question. Was it the overriding of the function name with the internal local variable?
$endgroup$
– Graipher
Jan 19 '17 at 12:57
$begingroup$
I mean avoid reassign variables already assigned.
$endgroup$
– FXux
Jan 19 '17 at 13:09
$begingroup$
I mean avoid reassign variables already assigned.
$endgroup$
– FXux
Jan 19 '17 at 13:09
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fcodereview.stackexchange.com%2fquestions%2f153029%2frecursively-list-files-within-a-directory%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