-2

I am using the python plugin for notepad ++ and need to be able to get a printout of all the files in a directory. Basically I need to run something like dir in command prompt.

Is there any way to do this?

rohits
  • 27
  • 1
  • 7
  • 1
    You mean [`os.listdir('.')`](https://docs.python.org/3/library/os.html#os.listdir)? – abarnert Jul 23 '14 at 23:32
  • If by "a printout", you mean something formatted, and with a bunch of additional columns, etc., just like `dir`, then you'll need to use `os.stat` on each one and format the columns yourself—or, of course, you can use `subprocess.check_output` to just run the command prompt's `dir` command. – abarnert Jul 23 '14 at 23:34

2 Answers2

2

You can use the os.listdir() function to get a list of the files in a directory.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
1

So, here's how to put all the files in a directory and in all its sub-directories in a list, print out the list.

Note: How to 'walk' was found here:

concatenate the directory and file name

# Task: Get a printout of all the files in a directory.

import os

# The directory that we are interested in
myPath = "/users/george/documents/"

# All the file paths will be stored in this list
filesList= []

for path, subdirs, files in os.walk(myPath):
    for name in files:
        filesList.append(os.path.join(path, name))

for i in filesList:
    print (str(i))
Community
  • 1
  • 1
GeorgeZ
  • 51
  • 5