Using pysmbc to read files over samba

后端 未结 5 2171
忘了有多久
忘了有多久 2020-12-30 13:12

I am using the python-smbc library on Ubuntu to access a samba share. I can access the directory structure fine, I am however not sure how to access actual files and their c

5条回答
  •  天命终不由人
    2020-12-30 14:06

    I also have had trouble using smbfs (random system lockdowns and reboots) and needed a quick answer.

    I've also tried the smbc module but couldn't get any data with it. I went just as far as accessing the directory structure, just like you.

    Time was up and I had to deliver the code, so I took a shortcut:

    I wrote a small wrapper around a "smbclient" call. It is a hack, ugly, really ugly, but it works for my needs. It is being used in production on the company I work.

    Here's some example usage:

    >>> smb = smbclient.SambaClient(server="MYSERVER", share="MYSHARE", 
                                    username='foo', password='bar', domain='baz')
    >>> print smb.listdir(u"/")
    [u'file1.txt', u'file2.txt']
    >>> f = smb.open('/file1.txt')
    >>> data = f.read()
    >>> f.close()
    >>> smb.rename(u'/file1.txt', u'/file1.old')
    

    The programmer before me was using a "bash" file with lots of smbclient calls, so I think my solution is at least better.

    I have uploaded it here, so you can use it if you want. Bitbucket repository is here. If you find a better solution please tell me and I'll replace my code too.

提交回复
热议问题