Using pysmbc to read files over samba

后端 未结 5 2166
忘了有多久
忘了有多久 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:13

    If don't know if this is more clearly stated, but here's what I gleaned from this page and sorted out from a little extra Google-ing:

    def do_auth (server, share, workgroup, username, password):
      return ('MYDOMAIN', 'myacct', 'mypassword')
    
    
    # Create the context
    ctx = smbc.Context (auth_fn=do_auth)
    destfile = "myfile.txt"
    source = open('/home/myuser/textfile.txt', 'r')
    # open a SMB/CIFS file and write to it
    file = ctx.open ('smb://server/share/folder/'+destfile, os.O_CREAT | os.O_WRONLY)
    for line in source:
            file.write (line)
    file.close()
    source.close()
    
    # open a SMB/CIFS file and read it to a local file
    source = ctx.open ('smb://server/share/folder/'+destfile, os.O_RDONLY)
    destfile = "/home/myuser/textfile.txt"
    fle = open(destfile, 'w')
    for line in source:
            file.write (line)
    file.close()
    source.close()
    

提交回复
热议问题