Wizard
Wizard

Reputation: 22083

Check multiple files if of the same

I have a paths_list,assuming they are of the same.

In [96]: len(paths_list)
Out[96]: 191
In [97]: paths_list
Out[97]:
['~/Desktop/Dev/sample.py',
 '~/Library/Containers/com.apple.CloudPhotosConfiguration/Data/Desktop/Dev/sample.py',
 '~/Library/Containers/com.apple.PressAndHold/Data/Desktop/Dev/sample.py',
 '~/Library/Containers/com.apple.iCal.CalendarNC/Data/Desktop/Dev/sample.py',
 '~/Library/Containers/com.apple.languageassetd/Data/Desktop/Dev/sample.py',
 '~/Library/Containers/com.apple.photos.VideoConversionService/Data/Desktop/Dev/sample.py',
 '~/Library/Containers/com.apple.iCal/Data/Desktop/Dev/sample.py',
 '~/Library/Containers/com.apple.share.Video.upload-Youku/Data/Desktop/Dev/sample.py',
 '~/Library/Containers/com.apple.QuickTimePlayerX/Data/Desktop/Dev/sample.py',
 '~/Library/Containers/com.apple.PassKit.PaymentAuthorizationUIExtension/Data/Desktop/Dev/sample.py',
 ...]

Tried to check them with a set

In [99]: { os.stat(i) for i in paths_list}
Out[99]: {os.stat_result(st_mode=33188, st_ino=8593437981, st_dev=16777220, 
st_nlink=1, st_uid=501, st_gid=20, st_size=554, st_atime=1510741689, 
st_mtime=1510453338, st_ctime=1510741688)}

The returned set contains only one item to conclude that they are some files.

I learn that os.path.samefile checking two paths each time. How to check the paths_list with os.path.samefile or other elegant methods?

Upvotes: 1

Views: 208

Answers (1)

georgexsh
georgexsh

Reputation: 16624

I am ok with your method, os.path.samefile() check st_ino and st_dev of both files, essentially same with your method, but if your really want to use samefile(), try this:

all(os.path.samefile(paths_list[i], paths_list[i+1])
    for i in range(len(paths_list)-1))

by making sure every neighbor pair of the file in your file list return True with samefile() call, you could say they are all of the same file.

Upvotes: 2

Related Questions