2

I am having an issue with trying to list all of the files/folders within a directory with Python 2.6, on Mac OS X.

To simplify the problem, I am attempting to simply list all the files on my desktop (which is not empty). I understand this can be done like this:

currentFileList = os.listdir("~/Desktop")

But I am getting the error:

currentFileList = os.listdir("~/Desktop")
OSError: [Errno 2] No such file or directory: '~/Desktop'

Any suggestions?

David Ferris
  • 2,215
  • 6
  • 28
  • 53

2 Answers2

8

You should pass absolute pass to os.listdir function. You can use os.expanduser function to expand ~:

os.listdir(os.path.expanduser('~/Desktop'))

By the way. Be careful: ~foobar will replace the path with home folder for user foobar (e.g. /home/foobar)

Daniil Ryzhkov
  • 7,416
  • 2
  • 41
  • 58
0

You need the full path not relative

os.listdir('/Users/YOURUSERNAME/Desktop') 
heinst
  • 8,520
  • 7
  • 41
  • 77