Hi again

I have another query I a struggling with that I would like to get some advice on.

Basically put as part of a process we have a number of shortcuts created. These are Windows shortcuts based on an item id - so an example would be 1000.lnk
Also there is a matching directory. It's not on a local machine but for testing I have created a small sample.

So I have a folder C:\Pyth\Shortcuts - which contains 4 shortcuts 1000.lnk, 1001.lnk, 1002.lnk, 1003.lnk
I have another folder C:\Pyth\Folders - which contains 3 folders 1000, 1001, 1002 (note there isn't a 1003 for testing purposes)

I need to copy the shortcut into the corresponding folder - i.e where the foldername matches the shortcut name (move 1000.lnk to folder \1000)

Can anyone reccomend a way of doing that? I've been doing some reading and I'm not sure whether is best to use glob, os.walk, fnmatch or some other method?

I had a look at fnmatch to see how it responds using the following quick portion of code:

import fnmatch
import os 

for file in os.listdir("C:\\Pyth\\Shortcuts\\"):
    if fnmatch.fnmatch(file, "*.lnk"):
        print file

for file2 in os.listdir("C:\\Pyth\\Folders\\"):
    if fnmatch.fnmatch(file2, "*"):
        print file2

if file == file2:
    print file
    print file2
    print "No match"

The fnmatch for file is resturning the filename with the corresponding file extension (.lnk). If I need to match filename and directory - am I going need to strip the file extension first and then do the match? I.e os split text...

It's a little open ended but I want to find the best method mainly - and to be honest I've no idea where to ideally start!


Use os.path.isdir() and os.path.islink() to identify which is which and then compare. As far as splitting goes, I would use something along these lines

import os
first, second = os.path.split(test_path)
print first, second
print second.split(".")
This article has been dead for over six months. Start a new discussion instead.