0

I've just starting using python 2.7 and was using the following code to ascertain the path to a file:

import os, fnmatch

#find the location of sunnyexplorer.exe
def find_files(directory, pattern):
    for root, dirs, files in os.walk(directory):
        for basename in files:
            if fnmatch.fnmatch(basename, pattern):
                filename = os.path.join(root, basename)
                yield filename

for filename in find_files('c:\users','*.sx2'): 
    print ('Found Sunny Explorer data in:', filename)

everthing seemed to be working fine until I tried to use the path and noticed an error. The program reports the path as:

c:\users\woody\Documents\SMA\Sunny Explorer

whereas the correct path is:

c:\users\woody\My Documents\SMA\Sunny Explorer
CraigTeegarden
  • 8,173
  • 8
  • 38
  • 43
  • There is nothing in the code here that'd produce an incorrect path. `os.walk()` uses `os.listdir()`, nothing else, so your OS reported that there is a `Documents` folder there. Are you certain there is *no* `Documents` folder in the `woody` folder? – Martijn Pieters Apr 13 '13 at 17:13
  • 1
    `My Documents` is called `Documents` in Windows Vista and 7, it's only the explorer showing you a different name. – filmor Apr 13 '13 at 17:17

1 Answers1

2

All versions of MS Windows since Vista store a user's documents in C:\Users\%username%\Documents by default.

However, they also include an NTFS junction point C:\Users\%username%\My Documents which points to the same location for backward compatibility.

The problem is, as I understand it, that you can't work with junction points with standard POSIX calls, so Python will not be able to use them without some Windows-specific extension module.

See also this question on superuser.com.

Community
  • 1
  • 1
Aya
  • 39,884
  • 6
  • 55
  • 55
  • Many thanks for all the help. Windows explorer does show the folder as being called "My Documents" but I guess it must be due to the backwards compatibility issue. Now I know whats happening I can fix it. Thanks once again ;-) Woody – user2277888 Apr 15 '13 at 06:49