0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Windows共有フォルダからデータをコピー

Posted at

目的
Windows共有フォルダからデータをコピーしたい。

準備
pip3からpysmbをインストールしておく

windows_copy

from smb.SMBConnection import SMBConnection
import platform


connection = SMBConnection(
    "USERNAME",             # ログインユーザ
    "PASSWORD",             # ログインパスワード
    platform.uname().node,  # 自ノード名
    "REMOTE_NODE_NAME",     # リモート端末名
    domain = "DOMAIN_NAME", # リモート端末の所属するドメイン名
    use_ntlm_v2 = True      # NTNMv2認証を利用
    )

connection.connect("IPADDRESS", 139) # IPアドレスとsmbで使用するポートを指定

# マウントするボリュームと対象パスを指定
files = connection.listPath("Users", "everyone/Desktop") 

# testが入るファイル名を取得してみる。
for a in files:
    print(type(a)) # -> <class 'smb.base.SharedFile'>
    if "test" in a.filename:
        print(a.filename) # -> ex) test.txt

# セッション断
connection.close()

a.filenameでファイル名を取得したが、ファイルサイズ等他の属性は以下を参照
http://pysmb.readthedocs.io/en/latest/api/smb_SharedFile.html

windows_copy

# testを含むファイルをコピー
for a in files:
    if "test" in a.filename:
        with open(a.filename, mode='wb') as file:
            connection.retrieveFile("Users", "everyone/Desktop/" + a.filename, file,)
        
connection.close()

備忘録代わりに。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?