2012-11-29 2 views
4

파일이 네트워크 드라이브에 있는지 여부를 어떻게 확인할 수 있습니까? 경로가 로컬 디스크에있는 것처럼 보이지만 경로의 디렉토리 중 하나는 사실 네트워크 드라이브에 대한 심볼릭 링크입니다.파일이 파이썬의 네트워크 드라이브에 있는지 확인합니다.

+0

의 파일에 모두있는 파일 또는 링크를 디렉토리 구조를 통해 이동하고 캡처 할 수 os.walk와 함께 자신에게 사용을 함께 취할 수 시작하려면 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

네트워크 파일 시스템과 기본 마운트 포인트 목록을 얻을 수 있다고 가정합니다. 마운트 포인트 또는 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 

당신은 어쩌면이 당신을 도울 것입니다 특정 네트워크 파일 공유

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 
관련 문제