Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need to read sub directories only

Status
Not open for further replies.

Tester_V

Technical User
Nov 22, 2019
54
US
Hi,
I'm trying to read subdirectories only (with the path) but the code I found also prints the top dir+path that is "C:\\01'
I probably could write the output to a file and then would remove the first line from it but it is kind of unnecessary work.
Thank you in advance.
Here is the code:
Python:
import os

given_path = 'C:\\01'
for path, dirs, files in os.walk(given_path):
    print ("This is path to:  " + path)
    for f in files:
        print ("These are files:"       +f)
    for d in dirs:
        print("These are Directories:"   + d)
 
Got an answer from somewhere else.
Here is the code:

Python:
import os

given_path = 'C:\\01'
for filename in os.listdir(given_path):
    filepath = os.path.join(given_path, filename)
    if os.path.isdir(filepath):
        print("SubDirectory name:", filename) 
        print("SubDirectory path:", filepath)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top