2017-12-12 16 views
1

subprocess 명령을 사용하여 SMB를 통해 연결하려고 시도하고 원격 디렉터리에 연결하려고합니다. 연결이 설정되면 디렉토리의 내용을 나열하려고합니다.SMB, 하위 프로세스 및 OS를 통한 디렉터리 내용 반환

from os import walk 


def getFileContents(directory): 
    f = [] 
    for (dirpath, dirnames, filenames) in walk(directory): 
     f.extend(filenames) 
     break 
    return f 

내가 파일 내용을 반환이 기대 해요 :

import os 
import shutil 
import subprocess 
from directory import getFileContents 
from os import walk 

def mount(remote_dir, local_dir): 
    retcode = subprocess.call(["/sbin/mount", "-t", "smbfs", remote_dir, local_dir]) 

def unmount(local_dir): 
    retcode = subprocess.call(["/sbin/umount", local_dir]) 

def getFileContents(directory): 
    f = [] 
    for (dirpath, dirnames, filenames) in walk(directory): 
     f.extend(filenames) 
     break 
    return f 

mount('//[email protected]/Pictures', 'Pictures') 

a = getFileContents('/Volumes/Pictures') 
print(a) 

input("Press any key to unmount: ") 


unmount('Pictures') 

이 코드는 디렉토리의 내용을 나열하도록되어 별도의 기능에서 가져 오기를 참조 : 여기에 코드입니다 원격 컴퓨터의 디렉토리 인 '/ Volumes/Pictures'폴더의 'a'내용을 인쇄 할 때 for 루프 외부에서 빈 목록을 반환합니다. 나는 OSX에서 일하고있다. 나는이 문제가 내 파일 경로가 어떻게 구조화되어 있는지에 관한 것이라고 믿는다. 디렉토리를 마운트하는 기능이 어떤 디렉토리에서 코드를 실행하는지에 기초하여 통과하거나 실패하는 것처럼 보인다. 내가 파이썬 터미널에 다음 실행하면 :

>>>from os import walk 


>>>def getFileContents(directory): 
    ...f = [] 
    ...for (dirpath, dirnames, filenames) in walk(directory): 
     ...f.extend(filenames) 
     ...break 
    ...return f 
a = getFileContents('/Volumes/Pictures') 
print(a) 

를 서브 프로세스를 통해 SMB 연결을 할 때 나는 디렉토리 내용의 목록을 다시 얻을 수 있지만, 수 없습니다. 내가 어디로 잘못 가고 있니?

답변

0

위의 코드에서 'local_dir'부분을 보는 방법에 오류가있었습니다. 위의 코드는 원격 디렉토리의 내용을 로컬 디렉토리에 마운트합니다. 따라서 코드를 다음과 같이 변경하면

import os 
import shutil 
import subprocess 
from directory import getFileContents 
from os import walk 

def mount(remote_dir, local_dir): 
    retcode = subprocess.call(["/sbin/mount", "-t", "smbfs", remote_dir, local_dir]) 

def unmount(local_dir): 
    retcode = subprocess.call(["/sbin/umount", local_dir]) 

def getFileContents(directory): 
    f = [] 
    for (dirpath, dirnames, filenames) in walk(directory): 
     f.extend(filenames) 
     break 
    return f 

mount('//[email protected]/Pictures', '/Users/ParanoidPenguin/smb') 

a = getFileContents('/Users/ParanoidPenguin/smb') 
print(a) 

input("Press any key to unmount: ") 


unmount('/Users/ParanoidPenguin/smb') 

이 문제를 해결할 수 있습니다. 이 경우 원격 'Pictures'폴더의 내용은 로컬 디렉토리 '/ Users/ParanoidPenguin/smb'에 표시됩니다.