通过 SMB、子进程和 OS 返回目录内容
Returning directory contents over SMB, subprocess, and OS
我正在尝试使用 subprocess 命令通过 SMB 连接,以建立与远程目录的连接。建立连接后,我想列出目录的内容。这是代码:
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('//Guest@192.168.1.167/Pictures', 'Pictures')
a = getFileContents('/Volumes/Pictures')
print(a)
input("Press any key to unmount: ")
unmount('Pictures')
该代码引用了来自单独函数的导入,该函数应该列出目录的内容:
from os import walk
def getFileContents(directory):
f = []
for (dirpath, dirnames, filenames) in walk(directory):
f.extend(filenames)
break
return f
我希望 return 文件夹“/Volumes/Pictures”的文件内容,这是远程计算机上的一个目录,但是,当只打印 [=22] 的内容时=],在 for 循环之外,我 return 返回一个空列表。我在 OSX 工作,我相信问题可能出在我的文件路径的结构上,因为挂载目录的能力似乎是根据我从哪个目录执行代码而通过或失败的.当我 运行 在 python 终端中输入以下内容时:
>>>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 连接时却不行。我哪里错了?
错误在于我查看上述代码的 '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('//Guest@192.168.1.167/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”中。
我正在尝试使用 subprocess 命令通过 SMB 连接,以建立与远程目录的连接。建立连接后,我想列出目录的内容。这是代码:
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('//Guest@192.168.1.167/Pictures', 'Pictures')
a = getFileContents('/Volumes/Pictures')
print(a)
input("Press any key to unmount: ")
unmount('Pictures')
该代码引用了来自单独函数的导入,该函数应该列出目录的内容:
from os import walk
def getFileContents(directory):
f = []
for (dirpath, dirnames, filenames) in walk(directory):
f.extend(filenames)
break
return f
我希望 return 文件夹“/Volumes/Pictures”的文件内容,这是远程计算机上的一个目录,但是,当只打印 [=22] 的内容时=],在 for 循环之外,我 return 返回一个空列表。我在 OSX 工作,我相信问题可能出在我的文件路径的结构上,因为挂载目录的能力似乎是根据我从哪个目录执行代码而通过或失败的.当我 运行 在 python 终端中输入以下内容时:
>>>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 连接时却不行。我哪里错了?
错误在于我查看上述代码的 '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('//Guest@192.168.1.167/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”中。