def ls(self, path, detail=True, **kwargs): unc = _as_unc_path(self.host, path) listed = smbclient.listdir(unc, **kwargs) dirs = ["/".join([path.rstrip("/"), p]) for p in listed] if detail: dirs = [self.info(d) for d in dirs] return dirs
def fetch_xxxx(self,remotedri,filetpye,filestring = "."): print('Start Download smbfile') logging.info('Start Download smbfile') Files = listdir(remotedri) TotalFiles = 0 SuccessFiles = 0 for onefile in Files : try : filecompile = re.compile('{}'.format(filestring)) findlength = len(filecompile.findall(onefile)) if onefile.lower().endswith('.{}'.format(filetpye)) and findlength > 0: Remotepath = os.path.join(remotedri,onefile) Localpath = './temporaryfile/{}'.format(onefile) TotalFiles+= 1 """通过创建好的SMB共享盘客服端,读取远端文件内容写入本地存储。Remotepath要读取的远程文件路径,Localpath是要写入内容的本地文件路径""" with open_file(Remotepath,mode = "rb") as fr: file_bytes = fr.read() with open(Localpath,"wb") as fw : fw.write(file_bytes) SuccessFiles+= 1 except Exception as inst : print('Download file Fail') print(inst) print("smb download Total Number of Files: "+ str(TotalFiles) +"; Success Files: " +str(SuccessFiles)) logging.info("smb download Total Number of Files: "+ str(TotalFiles) +"; Success Files: " +str(SuccessFiles))
def listDirectory(self, dir: str) -> List[str]: try: return smbclient.listdir( dir, username=self._username, password=self._password ) except smbprotocol.exceptions.SMBResponseException as exc: raise BackendError(exc.message) except smbprotocol.exceptions.SMBException as exc: raise BackendError(str(exc))
def download_file(self, remotedri, filetpye, filestring="."): """ param:remotedri --> "10.41.52.124/mm/Scrap/output" filetpye --> "xlsx" filestring = "." --> filestring = "Scrap" 该文档具有的唯一特征。 fun:从SMB指定目录下,下载指定类型、指定特征的一个文档,到smbtemporaryfile文件夹。 return: 返回下载的文档名称 :-->"NPI_PRINFO1.xls" onefile(包含filetype) """ print('Start To Download smbfile') logging.info('Start To Download smbfile') Files = listdir(remotedri) TotalFiles = 0 SuccessFiles = 0 for onefile in Files: try: filecompile = re.compile('{}'.format(filestring)) findlength = len(filecompile.findall(onefile)) if onefile.lower().endswith( '.{}'.format(filetpye)) and findlength > 0: Remotepath = os.path.join(remotedri, onefile) Localpath = './smbtemporaryfile/{}'.format(onefile) TotalFiles += 1 with open_file(Remotepath, mode="rb") as fr: file_bytes = fr.read() with open(Localpath, "wb") as fw: fw.write(file_bytes) SuccessFiles += 1 except Exception as inst: print('Fail To Download smbfile ') print(inst) print("smb download Total Number of Files: " + str(TotalFiles) + "; Success Files: " + str(SuccessFiles)) logging.info("smb download Total Number of Files: " + str(TotalFiles) + "; Success Files: " + str(SuccessFiles)) return onefile
def listdir(self, path): return smbclient.listdir(self._join_path(path), **self._conn_kwargs)
from smbclient import ( listdir, mkdir, register_session, rmdir, scandir, ) # Optional - register the server with explicit credentials register_session("server", username="******", password="******") # Create a directory (only the first request needs credentials) mkdir(r"\\server\share\directory", username="******", password="******") # Remove a directory rmdir(r"\\server\share\directory") # List the files/directories inside a dir for filename in listdir(r"\\server\share\directory"): print(filename) # Use scandir as a more efficient directory listing as it already contains info like stat and attributes. for file_info in scandir(r"\\server\share\directory"): file_inode = file_info.inode() if file_info.is_file(): print("File: %s %d" % (file_info.name, file_inode)) elif file_info.is_dir(): print("Dir: %s %d" % (file_info.name, file_inode)) else: print("Symlink: %s %d" % (file_info.name, file_inode))