1

How to read all files and also the filenames? I am using MAC so is there any there a different way to give path on MAC in Python?

user2793286
  • 179
  • 2
  • 14

1 Answers1

1

Maybe something like this? Or os.listdir() is simpler if you don't need recursion.

Even on Windows, Python abstracts away the differences between operating systems if you use it well.

#!/usr/local/cpython-3.3/bin/python

import os

def main():
    for root, dirs, files in os.walk('/home/dstromberg/src/outside-questions'):
        for directory in dirs:
            print('directory', os.path.join(root, directory))
        for file_ in files:
            print('file', os.path.join(root, file_))

main()

See http://docs.python.org/3/library/os.html for more info.

dstromberg
  • 6,954
  • 1
  • 26
  • 27