0

I'm trying to write a python script that go through all directories in the same directory as the script currently resides, their subdirectories and files in the directories with the suffix ".symlink" and later create symbolic links in the home directory.

But I got a problem. The script don't find any directories or files. I probably misusing the walk method. Any suggestions?

import os

class Symlink:
    """A reference to a file or (sub)directory in the filesystem "marked" to be linked to from the user's home directory.'"""

    def __init__(self, targetPath, linkName):
        self.targetPath = targetPath
        self.linkName = linkName

    def getTargetPath(self):
        return self.targetPath

    def getLinkName(self):
        return self.linkName

    def linkExists(self, linkName):
        return os.path.exists(os.path.join(os.path.expanduser('~'), linkName))

    def createSymlink(self):
        overwrite, skip = False, False

        answer = ''
        while True:
            try:
                if linkExists(self.getLinkName()) and \
                    not overwriteAll and \
                    not skipAll:
                    answer = input('A file or link already exists in your home directory with the name', linkName, '. What do you want to do? [o]verwrite, [O]verwrite all, [s]kip or [S]kip all?')

                if not answer in ['o', 'O', 's', 'S']:
                    raise ValueError(answer)

                break

            except ValueError as err:
                print('Error: Wrong answer:', err)

        if answer == 'o':
            overwrite = True
        if answer == 'O':
            overwriteAll = True
        if answer == 's':
            skip = True
        if answer == 'S':
            skipAll = True

        if overwrite or overwriteAll:
            os.symlink(self.getTargetPath(), self.getLinkName())

def main():
    symlinks = []

    print('Adding directories and files to list...')
    currentDirectory = os.path.realpath(__file__)

    # Going throu this file's current directory and it's subdirs and files
    for dir_, directories, files in os.walk(currentDirectory):

        # For every subdirectory
        for dirName in directories:

            # Check if directory is marked for linking
            if dirName[-8:] == '.symlink':
                symlink = Symlink(os.path.join(dir_, dirName),  os.path.join(os.path.expanduser('~'), dirName[:-8]))

                # Add link to list of symbolic links to be made
                symlinks.append(symlink)

        # For every file in the subdirectory
        for fileName in files:

            # Check if file is marked for linking
            if fileName[-8:] == '.symlink':
                symlink = Symlink(os.path.join(dir_, fileName), os.path.join(os.path.expanduser('~'), fileName[:-8]))

                # Add link to list of symbolic links to be made
                symlinks.append(symlink)

    print(symlinks)
    print('Creating symbolic links...')
    overwriteAll, skipAll = False, False
    for link in symlinks:
        link.createSymlink()

    print("\nInstallation finished!")
fuesika
  • 3,280
  • 6
  • 26
  • 34

1 Answers1

0
currentDirectory = os.path.realpath(__file__)

I guess this is the problem - currentDirectory is the path to the script itself, not to the script's parent directory. You need also to call os.path.dirname()

currentDirectory = os.path.dirname(os.path.realpath(__file__))

See Find current directory and file's directory

Community
  • 1
  • 1
Anton Melnikov
  • 1,048
  • 7
  • 21