Reputation: 9179
I am making a python script that in the case of EXT filesystem, will create symbolic links of some stuff, otherwise it will move the files.
How can I know the type of the filesystem of a directory?
Upvotes: 2
Views: 267
Reputation: 41486
A variation on the accepted answer that checks if a particular directory is on a filesystem that supports symlinks (and handles cleaning up after itself):
import os, pathlib, tempfile
def supports_symlinks(target_dir) -> bool:
with tempfile.TemporaryDirectory(dir=target_dir) as link_check_dir:
link_check_path = pathlib.Path(link_check_dir)
link_path = link_check_path / "src"
try:
os.symlink(link_path, "dest")
except OSError:
# Failed to create symlink under the target path
return False
# Successfully created a symlink under the target path
return True
This is most useful when setting the symlink
option on shutil.copytree
rather than when making a single specific symlink.
Upvotes: 0
Reputation: 2086
some explicit code using @Joachim Isaksson's suggestion:
import os
try:
os.symlink("src", "dest")
except OSError:
print "cant do it :("
Upvotes: 4
Reputation: 180867
What you probably should do is to just try to make the link and if it fails, copy.
It'll give you the advantage that you'll automatically support all file systems with soft links without having to do advanced detection or keeping an updated list of supported file systems.
Upvotes: 4