4

I have 50+ folders, each of which contain a large amount of data that needs to be processed. All of them are processed using the exact same code, utilizing os.path.dirname(os.path.realpath(file)) to get the directory in which the python script is located so there is no manual editing needed by the user, they just need to double click.

I need the script to appear to be in each folder while actually only being in 1 place, so I can edit it once and then when it is run from any of these locations have the folder path be correct. The alternative is editing the master and then pasting one folder at a time though all 50+ folders any time I update the code, which is very very tedious and error prone. On Linux I could set this up with a symbolic link, but I can't figure out a way to do it on windows.

Alternately, a way to paste the file into all the target directories at once, instead of one at a time, would accomplish the same goal.

13

You need a symbolic link or a hard link.

Symbolic links (or symlinks for short) are quite similar to shortcuts: there's one actual file and multiple references (symlinks) to it. They even have that little arrow icon on them. Unlike shortcuts, symlinks can have any extension.

Hard links bind file on a hard drive to a location in the directory tree. Each file has at least one hard link, otherwise it wouldn't exist in any directory. If a file has multiple hard links, the original one cannot be distinguished and the file physically exists in only one location.

Both have their limitations:

  • Some software doesn't play nicely with symlinks
  • Deleting original file leaves all its symlinks broken
  • You cannot hard link folders (but you can create a directory junction if symlink is not enough)
  • Creating cross-partition hard links is impossible

Symlinks are usually sufficient.

To create a symlink or a hard link:

  1. Launch privileged command line: press Win, type cmd, press Ctrl+Shift+Enter
  2. Issue mklink command:

    • mklink link_name link_target for file symlink
    • mklink /d link_name link_target for folder symlink
    • mklink /h link_name link_target for file hard link
    • mklink /j link_name link_target for directory junction
  • I would also point out that hard links require extra effort to delete completely. – user76225 Feb 18 '15 at 3:06
  • "If a file has multiple hard links, the original one cannot be distinguished" Why? – Pacerier Feb 18 '15 at 4:56
  • @Pacerier because they are all identical, except for the path. Here is a file and a hardlink: (screenshot). Symlinks would appear as <SYMLINK> in dir and have arrow icon in Explorer, they are kind of special file type. Hard links aren't special in any way.(diagram) – gronostaj Feb 18 '15 at 7:19
  • From Raymond Chen, The Old New Thing: "Recall that hard links create an alternate name for a file. Once that alternate name is created, there is no way to tell which is the original name and which is the new name. The new file does not have a "link back to the original"; they are both links to the underlying file content." blogs.msdn.com/b/oldnewthing/archive/2011/07/20/10188033.aspx – user1008646 Feb 18 '15 at 9:36
  • @user1008646 & gronostaj, But isn't it possible to observe the "created" timestamp to deduce which file may be the first one? – Pacerier Feb 22 '15 at 21:40
1

On Win Vista and newer, you can create sym links or hard links, which behave as you'd expect on Linux. You can create a hard link or sym link by using the MKLINK command built in Windows Visa and newer. (Making sym links require admin privilege.)

Or, for the alternative you mentioned, if the folders are simply the immediate subfolders of the folder where the script resides, you can do in the command prompt at the script's folder:

for /d %D in (*) do (copy /y "myscript.py" "%D")

to update them in one go. If writing this command in batch, you need to use the following code instead:

for /d %%D in (*) do (copy /y "myscript.py" "%%D")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.