Python wrapper for `baidu webdisk <https://pan.baidu.com>`.
Project description
百度网盘 Web API 的 Python 封装
安装
通过 pypi
pip install -U python-dupan
入门介绍
1. 导入模块和创建实例
导入模块
from dupan import DuPanClient, DuPanFileSystem
创建客户端对象,需要传入 cookie,如果不传或者 cookie 失效,则需要扫码登录
cookie = "BDUSS=...;STOKEN=..."
client = DuPanClient(cookie)
创建文件系统对象
fs = DuPanFileSystem(client)
或者直接在 client 上就可获取文件系统对象
fs = client.fs
或者直接用 DuPanFileSystem 登录
fs = DuPanFileSystem.login(cookie)
2. 操作网盘使用 Python 式的文件系统方法
文件系统对象的方法,设计和行为参考了 os、posixpath、pathlib.Path 和 shutil 等模块。
dupan.DuPanFileSystem 实现了读写的文件系统方法。
| 方法 | 说明 | 参考 |
|---|---|---|
| __contains__() path in fs |
判断路径是否存在 | exists() |
| __delitem__() del fs[path] |
删除文件或目录 | rmtree() |
| __getitem__() fs[path] |
获取路径对应的 dupan.DuPanPath 对象 | as_path() |
| __iter__() iter(fs) |
遍历目录,获取 dupan.DuPanPath 对象的迭代器 | iter(max_depth=-1) |
| __itruediv__() fs /= path |
切换当前工作目录 | chdir() |
| __len__() len(fs) |
获取当前目录的直属文件和目录数 | |
| __repr__() repr(fs) |
获取对象的字符串表示 | |
| __setitem__() fs[path] = data |
替换文件或上传目录等 | touch() upload() upload_tree() write_bytes() write_text() |
| abspath() | 获取绝对路径 | posixpath.abspath |
| as_path() | 获取路径对应的 dupan.DuPanPath 对象 | |
| attr() | 获取文件或目录的属性 | |
| chdir() cd() |
切换当前工作目录 | os.chdir |
| copy() cp() |
复制文件 | shutil.copy |
| copytree() | 复制文件或目录 | shutil.copytree |
| download() | 下载文件 | 暂不可用 |
| download_tree() | 下载文件或目录 | 暂不可用 |
| exists() | 判断文件或目录是否存在 | posixpath.exists |
| getcwd() pwd() |
获取当前工作目录的路径 | os.getcwd |
| get_url() | 获取文件的下载链接 | |
| glob() | 遍历目录并用通配符模式筛选 | glob.iglob pathlib.Path.glob |
| isdir() | 判断是否存在且是目录 | posixpath.isdir |
| isfile() | 判断是否存在且是文件 | posixpath.isfile |
| is_empty() | 判断是否不存在、空文件或空目录 | |
| iter() | 遍历目录,获取 dupan.DuPanPath 对象的迭代器 | |
| iterdir() | 迭代目录,获取 dupan.DuPanPath 对象的迭代器 | pathlib.Path.iterdir |
| listdir() ls() |
罗列目录,获取文件名列表 | os.listdir |
| listdir_attr() la() |
罗列目录,获取文件属性 dict 列表 | |
| listdir_path() ll() |
罗列目录,获取 dupan.DuPanPath 对象列表 | |
| makedirs() | 创建多级的空目录 | os.makedirs |
| mkdir() | 创建空目录 | os.mkdir |
| move() mv() |
对文件或目录进行改名或移动,如果目标路径存在且是目录,则把文件移动到其中(但是目录中有同名的文件或目录,还是会报错) | shutil.move |
| open() | 打开文件(只支持读,如果要写,请用 write_bytes 或 upload 替代) | open io.open 暂不可用 |
| read_bytes() | 读取文件为二进制 | pathlib.Path.read_bytes 暂不可用 |
| read_bytes_range() | 读取文件为二进制 | 暂不可用 |
| read_block() | 读取文件为二进制 | 暂不可用 |
| read_text() | 读取文件为文本 | pathlib.Path.read_text 暂不可用 |
| remove() rm() |
删除文件 | os.remove |
| removedirs() | (自底向上地)删除多级的空目录 | os.removedirs |
| rename() | 对文件或目录进行改名或移动 | os.rename |
| renames() | 对文件或目录进行改名或移动,然后对原来所在目录执行 removedirs | os.renames |
| replace() | 对文件或目录进行改名或移动,并且如果原来路径上是文件,目标路径上也存在同名文件,则会先把后者删除 | os.replace |
| rglob() | 遍历目录并用通配符模式筛选 | pathlib.Path.rglob |
| rmdir() | 删除空目录 | os.rmdir |
| rmtree() | 删除文件或目录 | shutil.rmtree |
| scandir() | 迭代目录,获取 dupan.DuPanPath 对象的迭代器 | os.scandir |
| stat() | 获取文件状态 os.stat_result 信息 | os.stat |
| touch() | 访问路径,如果不存在则新建空文件 | pathlib.Path.touch 暂不可用 |
| upload() | 上传文件 | 暂不可用 |
| upload_tree() | 上传目录 | 暂不可用 |
| unlink() | 删除文件 | os.unlink |
| walk() | 遍历目录,获取文件名列表 | os.walk |
| walk_attr() | 遍历目录,获取文件属性 dict 列表 | |
| walk_path() | 遍历目录,获取 dupan.DuPanPath 对象列表 | |
| write_bytes() | 向文件写入二进制 | pathlib.Path.write_bytes 暂不可用 |
| write_text() | 向文件写入文本 | pathlib.Path.write_text 暂不可用 |
dupan.DuPanPath 实现了二次封装,从路径的角度来进行操作。
3. 遍历文件系统和查找文件
1. 获取当前目录下所有 .mkv 文件的 url
第 1 种方法,使用 iter,返回 dupan.DuPanPath 对象的迭代器
for path in fs.iter(max_depth=-1):
if path.name.endswith(".mkv"):
print(path.url)
第 2 种方法,使用 glob,参考 pathlib.Path.glob 和 glob.iglob,使用通配符查找
for path in fs.glob("**/*.mkv"):
print(path.url)
第 3 种方法,使用 rglob,参考 pathlib.Path.rglob
for path in fs.rglob("*.mkv"):
print(path.url)
文档
正在编写中
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_dupan-0.0.2.2.tar.gz.
File metadata
- Download URL: python_dupan-0.0.2.2.tar.gz
- Upload date:
- Size: 28.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.13.5 Darwin/25.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00b8eec3acc4207e939d8342a46685a8bb16fc3485b161f9d7c9c2ecebb90a45
|
|
| MD5 |
5b7c2708111f4e7756bc4c02ab7d483f
|
|
| BLAKE2b-256 |
16b317f36556905f1f3a2dc0266bb893417d2ae7e76ff937072da1e1738bbafd
|
File details
Details for the file python_dupan-0.0.2.2-py3-none-any.whl.
File metadata
- Download URL: python_dupan-0.0.2.2-py3-none-any.whl
- Upload date:
- Size: 30.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.13.5 Darwin/25.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dad1b05ccee1bfd819d0bc40d89cbf1c7b2c80869d9af334475b8f56de604e23
|
|
| MD5 |
ea10f9e400652bb3dadc51dca642c46e
|
|
| BLAKE2b-256 |
077f2a283ed88ba709b34b628206c139a28f2c3603138bac1fb23a3441fc4221
|