Python failing to create symlinks

2.7k Views Asked by At

I'm trying to use python to make symbolic links. When I try to use os.symlink I get a WinError 5 "Access is denied" error, but if I try to create the same symlink using mklink, it works fine. (I'm running this as an administrator, and have the 'Create symbolic links' privilege.)

I then to use subprocess.call to call mklink directly with the proper arguments and such, and it runs without any errors, yet when it's finished the symlinks have not been created.

When I try to use Popen to run the command I get a 'system cannot find the file specified' error even though the directory is there.

This is on Windows Server 2008.

def createStudentFolders(studentList):
     grad_year = input("Please enter the year of graduation for the class of students being created (i.e. 2016): ")
     for student in studentList:

         student = student.replace(" ","") # Gets rid of the white space in the name
         studentPath = 'cpwd/{}'.format(student)

         try:
             os.mkdir(studentPath)
         except:
        pass



         print(studentPath)
         proc = Popen("icacls {} /inheritance:e /grant STUDENT\{}:(OI)(CI)F /T".format(studentPath, student))

         classFolder = "students/ClassOf{}".format(grad_year)

         try:
             os.mkdir(classFolder)
         except:
             pass



         os.symlink('{}/{}'.format(classFolder, student), studentPath, target_is_directory=True)


         print("Done")


def main():
   newStudentsLoc = "newStudents.txt"
   studentList = readStudentsToList(newStudentsLoc)
   createStudentFolders(studentList)

 main()
 input("Pausing")

File resides in Z:\

inside that are

cpwd\<student folders>
students\ClassOf<Year>\<symlinks to student folders go here>

I've tried using absolute paths as well with no luck.

0

There are 0 best solutions below