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 实现了读写的文件系统方法。
dupan.DuPanPath 实现了二次封装,从路径的角度来进行操作。
| 方法 | 说明 | 参考 |
|---|---|---|
| abspath | 获取绝对路径 | posixpath.abspath |
| as_path | 获取路径对应的 dupan.DuPanPath 对象 | |
| attr | 获取文件或文件夹的属性 | |
| chdir | 切换当前工作目录 | os.chdir |
| copy | 复制文件 | shutil.copy |
| copytree | 复制目录 | shutil.copytree |
| download | 下载文件 | 暂不可用 |
| download_tree | 下载目录 | 暂不可用 |
| exists | 判断文件或目录是否存在 | posixpath.exists |
| getcwd | 获取当前工作目录的路径 | os.getcwd |
| get_url | 获取文件的下载链接 | os.getcwd |
| glob | 用遍历一个目录并用通配符模式筛选 | pathlib.Path.glob |
| isdir | 判断是否目录 | posixpath.isdir |
| isfile | 判断是否文件 | posixpath.isfile |
| is_empty | 判断是否不存在、空文件或空文件夹 | posixpath.isfile |
| iter | 遍历一个目录 | |
| iterdir | 迭代一个目录 | pathlib.Path.iterdir |
| listdir | 罗列当前目录的文件名 | os.listdir |
| listdir_attr | 罗列当前目录时,还可以获取属性 | |
| listdir_path | 罗列当前目录时,还可以获取 dupan.DuPanPath 对象 | |
| makedirs | 创建多级的空目录 | os.makedirs |
| mkdir | 创建空文件夹 | os.mkdir |
| move | 对文件或文件夹进行改名或移动,目标路径存在且是一个目录,则把文件移动到其中(但是目录中有同名的文件或文件夹,还是会报错) | shutil.move |
| open | 打开文件(只支持读,不支持写) | open 暂不可用 |
| read_bytes | 读取文件为二进制 | pathlib.Path.read_bytes 暂不可用 |
| read_bytes_range | 读取文件为二进制 | 暂不可用 |
| read_block | 读取文件为二进制 | 暂不可用 |
| read_text | 读取文件为文本 | pathlib.Path.read_text 暂不可用 |
| remove | 删除一个文件 | os.remove |
| removedirs | (自底向上地)删除多级的空目录 | os.removedirs |
| rename | 对文件或文件夹进行改名或移动 | os.rename |
| renames | 对文件或文件夹进行改名或移动,并且在移动后如果原来所在目录为空,则会删除那个目录 | os.renames |
| replace | 对文件或文件夹进行改名或移动,并且如果原始路径上是文件,目标路径上也存在一个文件,则会先把目标路径上的文件删除 | os.replace |
| rglob | 用遍历一个目录并用通配符模式筛选 | pathlib.Path.rglob |
| rmdir | 删除空文件夹 | os.rmdir |
| rmtree | 删除文件或文件夹,并且在删除文件夹时,也删除其中的文件和文件夹 | shutil.rmtree |
| scandir | 迭代一个目录 | os.scandir |
| stat | 获取文件或文件夹的部分 | os.stat |
| touch | 获取文件或文件夹的部分 | pathlib.Path.touch 暂不可用 |
| upload | 上传一个文件 | 暂不可用 |
| upload_tree | 上传一个目录 | 暂不可用 |
| ulink | 删除一个文件 | os.ulink |
| walk | 遍历一个目录,获取文件名 | os.walk |
| walk_attr | 遍历一个目录时,获取属性字典 dict | |
| walk_path | 遍历一个目录时,获取 dupan.DuPanPath 对象 | |
| write_bytes | 向文件写入二进制 | pathlib.Path.write_bytes 暂不可用 |
| write_text | 向文件写入文本 | pathlib.Path.write_text 暂不可用 |
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.1.1.tar.gz.
File metadata
- Download URL: python_dupan-0.0.1.1.tar.gz
- Upload date:
- Size: 27.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.11.4 Darwin/21.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
afee09f38c69470fbc30767a4a54e60c60ca2e99b487f4bf7f2106302952ca54
|
|
| MD5 |
014e077dbea5b34fb1a9802c73bc8281
|
|
| BLAKE2b-256 |
3fc3420235f859ff97bd74ae6c5abbdeb2acd7fc67cce1c99c4d551946f4fb9f
|
File details
Details for the file python_dupan-0.0.1.1-py3-none-any.whl.
File metadata
- Download URL: python_dupan-0.0.1.1-py3-none-any.whl
- Upload date:
- Size: 30.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.11.4 Darwin/21.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d95c2a0150e60eb51ccf6d4399b12ec03b4940da95f46ea43e1d0d896690697c
|
|
| MD5 |
dc92ba2b874b7095ba9d16079c6baf4c
|
|
| BLAKE2b-256 |
4993b604a6f7b3a841c7b430482a8554bf1241a4d7a6eac2c7f88b5b7efba0e2
|