git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Symlinks already present


On 27/07/2020 00:56, Termoregolato wrote:
> There is any way to check if? a directory is already symlinked, without 
> controlling every symlink viewing the link? That is a bit time 
> consuming, due I've two or three directory that can have a new symlink, 
> but I've to check on a list of 20-30000 symlinks to delete it and avoid 
> duplicates...


Please review "os ? Miscellaneous operating system interfaces" 
(https://docs.python.org/3/library/os.html)
- in particular os.stat() - and thus os.stat_result,
and given what you appear to be doing, possibly os.scandir() and 
os.DirEntry.


I built a test:

dir1
-- dir-link = symlink to dir2
dir2
-- real-file

 >>> os.stat( "dir2", follow_symlinks=True )
os.stat_result(st_mode=16893, st_ino=2345143, st_dev=64773, st_nlink=2, 
st_uid=1000, st_gid=1000, st_size=4096, st_atime=1595793224, 
st_mtime=1595793223, st_ctime=1595793223)
 >>> os.stat( "dir1/dir-link", follow_symlinks=True )
os.stat_result(st_mode=16893, st_ino=2345143, st_dev=64773, st_nlink=2, 
st_uid=1000, st_gid=1000, st_size=4096, st_atime=1595793224, 
st_mtime=1595793223, st_ctime=1595793223)
 >>> os.stat( "dir1/dir-link", follow_symlinks=False )
os.stat_result(st_mode=41471, st_ino=2345146, st_dev=64773, st_nlink=1, 
st_uid=1000, st_gid=1000, st_size=7, st_atime=1595793558, 
st_mtime=1595793558, st_ctime=1595793558)

NB st_size
     Size of the file in bytes, if it is a regular file or a symbolic 
link. The size of a symbolic link is the length of the pathname it 
contains, without a terminating null byte.

Thus, compare the results of the two calls to detect a difference.
-- 
Regards =dn