Recursively enter each subdirectory of a directory in Python [duplicate]












0
















This question already has an answer here:




  • Using os.walk() to recursively traverse directories in Python

    10 answers




I am new to Python and I am trying to write a function that will be able to enter inside a folder if there all files it should just print their names if it is a folder it should go inside it and print it's files, if there is a folder inside this folder it should also go inside and do that until there is nothing left. For now I haven't found a way to go that deep. Is there a way to do that recursively? How should I proceed my code for some reason doesn't enter all subdirectories. Thanks in advance



def list_files(startpath, d):
for root, dirs, files in os.walk(startpath):
for f in files:
print (f)
for di in dirs:
print (di)
list_files(di, d + 1)

list_files(path, 0)









share|improve this question













marked as duplicate by slider, paxdiablo python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 5:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • regular approach is create a function that deal with a folder at one level, then call it recursively when you see a sub folder.

    – Dong Nguyen
    Nov 26 '18 at 5:09











  • thanks, but I was hoping my function was doing that but for some reason it is not. or should I create a variable path = root + '/' + dir and each time a new dir is added on the top of that add it to my variable path and pass it to my function?

    – Victory Salta
    Nov 26 '18 at 5:12











  • I don't think you need to make a recursive call with os.walk. Remove the recursive call in the second for loop.

    – slider
    Nov 26 '18 at 5:17
















0
















This question already has an answer here:




  • Using os.walk() to recursively traverse directories in Python

    10 answers




I am new to Python and I am trying to write a function that will be able to enter inside a folder if there all files it should just print their names if it is a folder it should go inside it and print it's files, if there is a folder inside this folder it should also go inside and do that until there is nothing left. For now I haven't found a way to go that deep. Is there a way to do that recursively? How should I proceed my code for some reason doesn't enter all subdirectories. Thanks in advance



def list_files(startpath, d):
for root, dirs, files in os.walk(startpath):
for f in files:
print (f)
for di in dirs:
print (di)
list_files(di, d + 1)

list_files(path, 0)









share|improve this question













marked as duplicate by slider, paxdiablo python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 5:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • regular approach is create a function that deal with a folder at one level, then call it recursively when you see a sub folder.

    – Dong Nguyen
    Nov 26 '18 at 5:09











  • thanks, but I was hoping my function was doing that but for some reason it is not. or should I create a variable path = root + '/' + dir and each time a new dir is added on the top of that add it to my variable path and pass it to my function?

    – Victory Salta
    Nov 26 '18 at 5:12











  • I don't think you need to make a recursive call with os.walk. Remove the recursive call in the second for loop.

    – slider
    Nov 26 '18 at 5:17














0












0








0









This question already has an answer here:




  • Using os.walk() to recursively traverse directories in Python

    10 answers




I am new to Python and I am trying to write a function that will be able to enter inside a folder if there all files it should just print their names if it is a folder it should go inside it and print it's files, if there is a folder inside this folder it should also go inside and do that until there is nothing left. For now I haven't found a way to go that deep. Is there a way to do that recursively? How should I proceed my code for some reason doesn't enter all subdirectories. Thanks in advance



def list_files(startpath, d):
for root, dirs, files in os.walk(startpath):
for f in files:
print (f)
for di in dirs:
print (di)
list_files(di, d + 1)

list_files(path, 0)









share|improve this question















This question already has an answer here:




  • Using os.walk() to recursively traverse directories in Python

    10 answers




I am new to Python and I am trying to write a function that will be able to enter inside a folder if there all files it should just print their names if it is a folder it should go inside it and print it's files, if there is a folder inside this folder it should also go inside and do that until there is nothing left. For now I haven't found a way to go that deep. Is there a way to do that recursively? How should I proceed my code for some reason doesn't enter all subdirectories. Thanks in advance



def list_files(startpath, d):
for root, dirs, files in os.walk(startpath):
for f in files:
print (f)
for di in dirs:
print (di)
list_files(di, d + 1)

list_files(path, 0)




This question already has an answer here:




  • Using os.walk() to recursively traverse directories in Python

    10 answers








python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 5:03









Victory SaltaVictory Salta

306




306




marked as duplicate by slider, paxdiablo python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 5:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by slider, paxdiablo python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 5:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • regular approach is create a function that deal with a folder at one level, then call it recursively when you see a sub folder.

    – Dong Nguyen
    Nov 26 '18 at 5:09











  • thanks, but I was hoping my function was doing that but for some reason it is not. or should I create a variable path = root + '/' + dir and each time a new dir is added on the top of that add it to my variable path and pass it to my function?

    – Victory Salta
    Nov 26 '18 at 5:12











  • I don't think you need to make a recursive call with os.walk. Remove the recursive call in the second for loop.

    – slider
    Nov 26 '18 at 5:17



















  • regular approach is create a function that deal with a folder at one level, then call it recursively when you see a sub folder.

    – Dong Nguyen
    Nov 26 '18 at 5:09











  • thanks, but I was hoping my function was doing that but for some reason it is not. or should I create a variable path = root + '/' + dir and each time a new dir is added on the top of that add it to my variable path and pass it to my function?

    – Victory Salta
    Nov 26 '18 at 5:12











  • I don't think you need to make a recursive call with os.walk. Remove the recursive call in the second for loop.

    – slider
    Nov 26 '18 at 5:17

















regular approach is create a function that deal with a folder at one level, then call it recursively when you see a sub folder.

– Dong Nguyen
Nov 26 '18 at 5:09





regular approach is create a function that deal with a folder at one level, then call it recursively when you see a sub folder.

– Dong Nguyen
Nov 26 '18 at 5:09













thanks, but I was hoping my function was doing that but for some reason it is not. or should I create a variable path = root + '/' + dir and each time a new dir is added on the top of that add it to my variable path and pass it to my function?

– Victory Salta
Nov 26 '18 at 5:12





thanks, but I was hoping my function was doing that but for some reason it is not. or should I create a variable path = root + '/' + dir and each time a new dir is added on the top of that add it to my variable path and pass it to my function?

– Victory Salta
Nov 26 '18 at 5:12













I don't think you need to make a recursive call with os.walk. Remove the recursive call in the second for loop.

– slider
Nov 26 '18 at 5:17





I don't think you need to make a recursive call with os.walk. Remove the recursive call in the second for loop.

– slider
Nov 26 '18 at 5:17












1 Answer
1






active

oldest

votes


















-1














May be you can check this answer:



Using os.walk() to recursively traverse directories in Python



which employs os.walk() method like this.



import os

# traverse root directory, and list directories as dirs and files as files
for root, dirs, files in os.walk("."):
path = root.split(os.sep)
print((len(path) - 1) * '---', os.path.basename(root))
for file in files:
print(len(path) * '---', file)





share|improve this answer
























  • Please don't copy/paste answers from a duplicate.

    – slider
    Nov 26 '18 at 5:12











  • initially I tried that but it doesn't enter all my directories either

    – Victory Salta
    Nov 26 '18 at 5:13


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














May be you can check this answer:



Using os.walk() to recursively traverse directories in Python



which employs os.walk() method like this.



import os

# traverse root directory, and list directories as dirs and files as files
for root, dirs, files in os.walk("."):
path = root.split(os.sep)
print((len(path) - 1) * '---', os.path.basename(root))
for file in files:
print(len(path) * '---', file)





share|improve this answer
























  • Please don't copy/paste answers from a duplicate.

    – slider
    Nov 26 '18 at 5:12











  • initially I tried that but it doesn't enter all my directories either

    – Victory Salta
    Nov 26 '18 at 5:13
















-1














May be you can check this answer:



Using os.walk() to recursively traverse directories in Python



which employs os.walk() method like this.



import os

# traverse root directory, and list directories as dirs and files as files
for root, dirs, files in os.walk("."):
path = root.split(os.sep)
print((len(path) - 1) * '---', os.path.basename(root))
for file in files:
print(len(path) * '---', file)





share|improve this answer
























  • Please don't copy/paste answers from a duplicate.

    – slider
    Nov 26 '18 at 5:12











  • initially I tried that but it doesn't enter all my directories either

    – Victory Salta
    Nov 26 '18 at 5:13














-1












-1








-1







May be you can check this answer:



Using os.walk() to recursively traverse directories in Python



which employs os.walk() method like this.



import os

# traverse root directory, and list directories as dirs and files as files
for root, dirs, files in os.walk("."):
path = root.split(os.sep)
print((len(path) - 1) * '---', os.path.basename(root))
for file in files:
print(len(path) * '---', file)





share|improve this answer













May be you can check this answer:



Using os.walk() to recursively traverse directories in Python



which employs os.walk() method like this.



import os

# traverse root directory, and list directories as dirs and files as files
for root, dirs, files in os.walk("."):
path = root.split(os.sep)
print((len(path) - 1) * '---', os.path.basename(root))
for file in files:
print(len(path) * '---', file)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 5:11









TaoTao

214




214













  • Please don't copy/paste answers from a duplicate.

    – slider
    Nov 26 '18 at 5:12











  • initially I tried that but it doesn't enter all my directories either

    – Victory Salta
    Nov 26 '18 at 5:13



















  • Please don't copy/paste answers from a duplicate.

    – slider
    Nov 26 '18 at 5:12











  • initially I tried that but it doesn't enter all my directories either

    – Victory Salta
    Nov 26 '18 at 5:13

















Please don't copy/paste answers from a duplicate.

– slider
Nov 26 '18 at 5:12





Please don't copy/paste answers from a duplicate.

– slider
Nov 26 '18 at 5:12













initially I tried that but it doesn't enter all my directories either

– Victory Salta
Nov 26 '18 at 5:13





initially I tried that but it doesn't enter all my directories either

– Victory Salta
Nov 26 '18 at 5:13





Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen