-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?

2
  • 1
    You mean os.listdir('.')? Commented Jul 23, 2014 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. Commented Jul 23, 2014 at 23:34

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.