#!/usr/bin/env python3.2
import os
import sys
fileList = []
rootdir = sys.argv[1]
for subdir, dirs, files in os.walk(rootdir, followlinks=True):
for file in files:
f = os.path.join(subdir,file)
if os.path.islink(file):
countlink = countlink+1
linkto = os.readlink(f)
print(linkto)
If i give this code a folder say /Current and files /Current/file.exe and a symlink /Current/link, the "islink" doesnt recognize the "link" symlink but considers it a directory and moves on to the actual file it links to. My requirement is to just stop when it finds a symlink and print it. i am using Python3.2
The problem seems to be that you're printing what readlink returns which is the name of the target. Additionally, you're printing every file in the middle loop. The True value for followLinks is causing you to recurse into directories that are symlinked. Lastly, any symlinks to directories are stored in dirs but not in files. The following should work:
for subdir, dirs, files in os.walk(rootdir, followlinks=False):
for file in files+dirs:
f = os.path.join(subdir,file)
if os.path.islink(file):
countlink = countlink+1
linkto = os.readlink(f)
print("{} -> {}".format(f,linkto))