Sushin K.Kumar
Sushin K.Kumar

Reputation: 149

how to get a folder name and file name in python

I have a python program named myscript.py which would give me the list of files and folders in the path provided.

import os
import sys

def get_files_in_directory(path):
    for root, dirs, files in os.walk(path):
        print(root)
        print(dirs)
        print(files)
path=sys.argv[1]
get_files_in_directory(path)

the path i provided is D:\Python\TEST and there are some folders and sub folder in it as you can see in the output provided below :

C:\Python34>python myscript.py "D:\Python\Test"
D:\Python\Test
['D1', 'D2']
[]
D:\Python\Test\D1
['SD1', 'SD2', 'SD3']
[]
D:\Python\Test\D1\SD1
[]
['f1.bat', 'f2.bat', 'f3.bat']
D:\Python\Test\D1\SD2
[]
['f1.bat']
D:\Python\Test\D1\SD3
[]
['f1.bat', 'f2.bat']
D:\Python\Test\D2
['SD1', 'SD2']
[]
D:\Python\Test\D2\SD1
[]
['f1.bat', 'f2.bat']
D:\Python\Test\D2\SD2
[]
['f1.bat']

I need to get the output this way :

D1-SD1-f1.bat
D1-SD1-f2.bat
D1-SD1-f3.bat
D1-SD2-f1.bat
D1-SD3-f1.bat
D1-SD3-f2.bat
D2-SD1-f1.bat
D2-SD1-f2.bat
D2-SD2-f1.bat

how do i get the output this way.(Keep in mind the directory structure here is just an example. The program should be flexible for any path). How do i do this. Is there any os command for this. Can you Please help me solve this? (Additional Information : I am using Python3.4)

Upvotes: 0

Views: 2525

Answers (2)

Nander Speerstra
Nander Speerstra

Reputation: 1526

To get what you want, you could do the following:

def get_files_in_directory(path):
    # Get the root dir (in your case: test)
    rootDir = path.split('\\')[-1]

    # Walk through all subfolder/files
    for root, subfolder, fileList in os.walk(path):
        for file in fileList:
            # Skip empty dirs
            if file != '':
                # Get the full path of the file
                fullPath = os.path.join(root,file)

                # Split the path and the file (May do this one and the step above in one go
                path, file = os.path.split(fullPath)

                # For each subfolder in the path (in REVERSE order)
                subfolders = []
                for subfolder in path.split('\\')[::-1]:

                    # As long as it isn't the root dir, append it to the subfolders list
                    if subfolder == rootDir:
                        break
                    subfolders.append(subfolder)

                # Print the list of subfolders (joined by '-')
                # + '-' + file
                print('{}-{}'.format( '-'.join(subfolders), file) )

path=sys.argv[1]
get_files_in_directory(path)

My test folder:

SD1-D1-f1.bat
SD1-D1-f2.bat
SD2-D1-f1.bat
SD3-D1-f1.bat
SD3-D1-f2.bat

It may not be the best way to do it, but it will get you what you want.

Upvotes: 0

Keith Hughitt
Keith Hughitt

Reputation: 4960

You could try using the glob module instead:

import glob
glob.glob('D:\Python\Test\D1\*\*\*.bat')

Or, to just get the filenames

import os
import glob
[os.path.basename(x) for x in glob.glob('D:\Python\Test\D1\*\*\*.bat')]

Upvotes: 1

Related Questions