2012-11-29 135 views
4

如何確定文件是否位於網絡驅動器上?我想包括路徑看起來像是在本地磁盤上的情況,但路徑中的一個目錄實際上是到網絡驅動器的符號鏈接。檢測文件是否在python中的網絡驅動器上

+0

也許這會幫助你開始: http://serverfault.com/questions/143084/how-can-i-check-whether-a-volume-is-mounted-where-it-is-supposed-to-be-using- pyt http://stackoverflow.com/questions/2889490/check-if-nfs-share-is-mounted-in-python-script –

回答

3

我打算假設您可以獲取網絡文件系統及其基本掛載點的列表,您可以通過解析mount或df來獲取它們。如果是這樣的話,你應該能夠做到你想用幾個不同的功能進行從os.path

這將需要的文件名,這是一個網絡文件系統的路徑一切。 path.realpath會將符號鏈接轉換爲它們鏈接到的文件的絕對路徑。

def is_netfile(fname, netfs): 
    fname = path.realpath(fname) 
    netfs = path.realpath(netfs) 
    if path.commonprefix([ netfs, fname ]) == netfs: 
     return True 
    else: 
     return False 

你可以把他的使用也隨着os.walk通過目錄結構所有可在文件或鏈接移動和捕捉到的文件在一個特定的網絡文件共享

start_dir = '/some/starting/dir' 
net1 = '/some/network/filesystem' 
remote_files = [] 

for root, dirs, files in os.walk(start_dir): 
    for f in files: 
     if is_netfile(path.join(root,f), net1): 
      remote_files.append(path.join(root,f)) 

print remote_files 
相關問題