how to open files that match a pattern in a subdirectory
Hello i have a problem finding and opening files in a subdirectory.
I have several different files called for example :
mouse_1_animal.txt
mouse_2_animal.txt
mouse_3_animal.txt
so i want to find all these files in the subdirectory of the working dir and open them and do something with there lines. This is my try:
i=1
for path, subdirs, files in os.walk(root) :
for file in files :
if file == "mouse_{0}_animal.txt".format(i) :
#do something
i = i + 1
but apparently it doesn't find all the files so i'm wondering if it's the way i'm using to find the file that is wrong.
python
add a comment |
Hello i have a problem finding and opening files in a subdirectory.
I have several different files called for example :
mouse_1_animal.txt
mouse_2_animal.txt
mouse_3_animal.txt
so i want to find all these files in the subdirectory of the working dir and open them and do something with there lines. This is my try:
i=1
for path, subdirs, files in os.walk(root) :
for file in files :
if file == "mouse_{0}_animal.txt".format(i) :
#do something
i = i + 1
but apparently it doesn't find all the files so i'm wondering if it's the way i'm using to find the file that is wrong.
python
I'd suggest to get a list of all files which match the pattern and then open them, edit and close one by one.
– Igor T.
Apr 9 '15 at 9:55
1
It may be easier to useos.listdir()
in case of a single subdirectory.
– user707650
Apr 9 '15 at 9:55
Note thatfiles
contains the filenames relative to the currentpath
whereos.walk
is at that moment; so you'll need to concatenate the directory and file name (os.path.join()
) to get the full path name.
– user707650
Apr 9 '15 at 9:58
stackoverflow.com/questions/14798220/…
– Christian K.
Apr 9 '15 at 9:59
add a comment |
Hello i have a problem finding and opening files in a subdirectory.
I have several different files called for example :
mouse_1_animal.txt
mouse_2_animal.txt
mouse_3_animal.txt
so i want to find all these files in the subdirectory of the working dir and open them and do something with there lines. This is my try:
i=1
for path, subdirs, files in os.walk(root) :
for file in files :
if file == "mouse_{0}_animal.txt".format(i) :
#do something
i = i + 1
but apparently it doesn't find all the files so i'm wondering if it's the way i'm using to find the file that is wrong.
python
Hello i have a problem finding and opening files in a subdirectory.
I have several different files called for example :
mouse_1_animal.txt
mouse_2_animal.txt
mouse_3_animal.txt
so i want to find all these files in the subdirectory of the working dir and open them and do something with there lines. This is my try:
i=1
for path, subdirs, files in os.walk(root) :
for file in files :
if file == "mouse_{0}_animal.txt".format(i) :
#do something
i = i + 1
but apparently it doesn't find all the files so i'm wondering if it's the way i'm using to find the file that is wrong.
python
python
asked Apr 9 '15 at 9:53
steTsteT
4919
4919
I'd suggest to get a list of all files which match the pattern and then open them, edit and close one by one.
– Igor T.
Apr 9 '15 at 9:55
1
It may be easier to useos.listdir()
in case of a single subdirectory.
– user707650
Apr 9 '15 at 9:55
Note thatfiles
contains the filenames relative to the currentpath
whereos.walk
is at that moment; so you'll need to concatenate the directory and file name (os.path.join()
) to get the full path name.
– user707650
Apr 9 '15 at 9:58
stackoverflow.com/questions/14798220/…
– Christian K.
Apr 9 '15 at 9:59
add a comment |
I'd suggest to get a list of all files which match the pattern and then open them, edit and close one by one.
– Igor T.
Apr 9 '15 at 9:55
1
It may be easier to useos.listdir()
in case of a single subdirectory.
– user707650
Apr 9 '15 at 9:55
Note thatfiles
contains the filenames relative to the currentpath
whereos.walk
is at that moment; so you'll need to concatenate the directory and file name (os.path.join()
) to get the full path name.
– user707650
Apr 9 '15 at 9:58
stackoverflow.com/questions/14798220/…
– Christian K.
Apr 9 '15 at 9:59
I'd suggest to get a list of all files which match the pattern and then open them, edit and close one by one.
– Igor T.
Apr 9 '15 at 9:55
I'd suggest to get a list of all files which match the pattern and then open them, edit and close one by one.
– Igor T.
Apr 9 '15 at 9:55
1
1
It may be easier to use
os.listdir()
in case of a single subdirectory.– user707650
Apr 9 '15 at 9:55
It may be easier to use
os.listdir()
in case of a single subdirectory.– user707650
Apr 9 '15 at 9:55
Note that
files
contains the filenames relative to the current path
where os.walk
is at that moment; so you'll need to concatenate the directory and file name (os.path.join()
) to get the full path name.– user707650
Apr 9 '15 at 9:58
Note that
files
contains the filenames relative to the current path
where os.walk
is at that moment; so you'll need to concatenate the directory and file name (os.path.join()
) to get the full path name.– user707650
Apr 9 '15 at 9:58
stackoverflow.com/questions/14798220/…
– Christian K.
Apr 9 '15 at 9:59
stackoverflow.com/questions/14798220/…
– Christian K.
Apr 9 '15 at 9:59
add a comment |
3 Answers
3
active
oldest
votes
The pythonic way:
import glob
for f in glob.glob('./subDir/mouse_*_animal.txt'):
# do_something
add a comment |
ok i've solvd my problem like this
file_list =
for name in glob.glob('./subDir/mouse_*_animal.txt'):
file_list.append(name)
for i in range(len(file_list)+1):
if './subDir/mouse_*_animal.txt'.format(i) in file_list:
#do something
1
glob.glob()
already returns a list of filenames. So you should shorten your code to:for f in glob.glob('./subDir/mouse_*_animal.txt'): do_something(f)
– haavee
Apr 9 '15 at 12:47
add a comment |
import fnmatch
import os
src = 'sourceDirPath'
for root, dirnames, filenames in os.walk(src):
for filename in fnmatch.filter(filenames, 'mouse*.txt'):
#do something
i = i + 1
for older python version you might want to try glob instead of fnmatch
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%2f29535254%2fhow-to-open-files-that-match-a-pattern-in-a-subdirectory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The pythonic way:
import glob
for f in glob.glob('./subDir/mouse_*_animal.txt'):
# do_something
add a comment |
The pythonic way:
import glob
for f in glob.glob('./subDir/mouse_*_animal.txt'):
# do_something
add a comment |
The pythonic way:
import glob
for f in glob.glob('./subDir/mouse_*_animal.txt'):
# do_something
The pythonic way:
import glob
for f in glob.glob('./subDir/mouse_*_animal.txt'):
# do_something
answered Apr 9 '15 at 12:48
haaveehaavee
3,8211620
3,8211620
add a comment |
add a comment |
ok i've solvd my problem like this
file_list =
for name in glob.glob('./subDir/mouse_*_animal.txt'):
file_list.append(name)
for i in range(len(file_list)+1):
if './subDir/mouse_*_animal.txt'.format(i) in file_list:
#do something
1
glob.glob()
already returns a list of filenames. So you should shorten your code to:for f in glob.glob('./subDir/mouse_*_animal.txt'): do_something(f)
– haavee
Apr 9 '15 at 12:47
add a comment |
ok i've solvd my problem like this
file_list =
for name in glob.glob('./subDir/mouse_*_animal.txt'):
file_list.append(name)
for i in range(len(file_list)+1):
if './subDir/mouse_*_animal.txt'.format(i) in file_list:
#do something
1
glob.glob()
already returns a list of filenames. So you should shorten your code to:for f in glob.glob('./subDir/mouse_*_animal.txt'): do_something(f)
– haavee
Apr 9 '15 at 12:47
add a comment |
ok i've solvd my problem like this
file_list =
for name in glob.glob('./subDir/mouse_*_animal.txt'):
file_list.append(name)
for i in range(len(file_list)+1):
if './subDir/mouse_*_animal.txt'.format(i) in file_list:
#do something
ok i've solvd my problem like this
file_list =
for name in glob.glob('./subDir/mouse_*_animal.txt'):
file_list.append(name)
for i in range(len(file_list)+1):
if './subDir/mouse_*_animal.txt'.format(i) in file_list:
#do something
answered Apr 9 '15 at 12:43
steTsteT
4919
4919
1
glob.glob()
already returns a list of filenames. So you should shorten your code to:for f in glob.glob('./subDir/mouse_*_animal.txt'): do_something(f)
– haavee
Apr 9 '15 at 12:47
add a comment |
1
glob.glob()
already returns a list of filenames. So you should shorten your code to:for f in glob.glob('./subDir/mouse_*_animal.txt'): do_something(f)
– haavee
Apr 9 '15 at 12:47
1
1
glob.glob()
already returns a list of filenames. So you should shorten your code to: for f in glob.glob('./subDir/mouse_*_animal.txt'): do_something(f)
– haavee
Apr 9 '15 at 12:47
glob.glob()
already returns a list of filenames. So you should shorten your code to: for f in glob.glob('./subDir/mouse_*_animal.txt'): do_something(f)
– haavee
Apr 9 '15 at 12:47
add a comment |
import fnmatch
import os
src = 'sourceDirPath'
for root, dirnames, filenames in os.walk(src):
for filename in fnmatch.filter(filenames, 'mouse*.txt'):
#do something
i = i + 1
for older python version you might want to try glob instead of fnmatch
add a comment |
import fnmatch
import os
src = 'sourceDirPath'
for root, dirnames, filenames in os.walk(src):
for filename in fnmatch.filter(filenames, 'mouse*.txt'):
#do something
i = i + 1
for older python version you might want to try glob instead of fnmatch
add a comment |
import fnmatch
import os
src = 'sourceDirPath'
for root, dirnames, filenames in os.walk(src):
for filename in fnmatch.filter(filenames, 'mouse*.txt'):
#do something
i = i + 1
for older python version you might want to try glob instead of fnmatch
import fnmatch
import os
src = 'sourceDirPath'
for root, dirnames, filenames in os.walk(src):
for filename in fnmatch.filter(filenames, 'mouse*.txt'):
#do something
i = i + 1
for older python version you might want to try glob instead of fnmatch
edited Nov 18 '18 at 23:45
e_i_pi
2,56021632
2,56021632
answered Apr 9 '15 at 10:23
kewlkievkewlkiev
117
117
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%2f29535254%2fhow-to-open-files-that-match-a-pattern-in-a-subdirectory%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
I'd suggest to get a list of all files which match the pattern and then open them, edit and close one by one.
– Igor T.
Apr 9 '15 at 9:55
1
It may be easier to use
os.listdir()
in case of a single subdirectory.– user707650
Apr 9 '15 at 9:55
Note that
files
contains the filenames relative to the currentpath
whereos.walk
is at that moment; so you'll need to concatenate the directory and file name (os.path.join()
) to get the full path name.– user707650
Apr 9 '15 at 9:58
stackoverflow.com/questions/14798220/…
– Christian K.
Apr 9 '15 at 9:59