Python开发比特币钱包网站无法找到Bitcoin Core的wallet.dat文件求助
解决Bitcoin Core钱包文件找不到的WalletError问题
关于报错提及bitcoinlib目录的原因
你用的Python库是bitcoinlib,它默认会在自身专属存储目录(比如Windows下的%APPDATA%\bitcoinlib\wallets)里查找钱包文件,不会自动识别Bitcoin Core原生钱包的路径。所以当代码没正确指定钱包来源时,它会去自己的目录找,自然找不到你在Bitcoin Core下创建的wallet.dat,因此报错会带上bitcoinlib相关目录。
具体解决步骤
1. 修正Python代码的RPC配置
如果你要连接的是Bitcoin Core中已创建的test2钱包,不要直接读取wallet.dat文件,而是通过RPC接口与Bitcoin Core交互。以下是修正后的代码示例:
from bitcoinlib.wallets import Wallet from bitcoinlib.services.bitcoind import BitcoindClient # 配置Bitcoin Core RPC参数,和你的bitcoin.conf保持一致 bitcoind_client = BitcoindClient( username="你的RPC用户名", # 对应bitcoin.conf里的rpcuser password="你的RPC密码", # 对应bitcoin.conf里的rpcpassword host="127.0.0.1", port=8332, # 主网用8332,测试网用18332,按实际配置修改 wallet_name="test2" # 直接填你在Bitcoin Core中创建的钱包名称 ) # 加载钱包 try: wallet = Wallet("test2", network="bitcoin", client=bitcoind_client) print("钱包加载成功") except Exception as e: print(f"错误详情: {e}")
2. 核对bitcoin.conf的RPC配置
确保你的C:\Users\MylesUsername\AppData\Roaming\Bitcoin\bitcoin.conf包含以下关键配置:
server=1 rpcuser=自定义用户名 rpcpassword=自定义密码 rpcallowip=127.0.0.1 # 可选:如果需要查询全量交易,加上txindex=1
修改后需要重启Bitcoin Core生效。
3. 验证RPC连接是否正常
在命令提示符里执行以下curl命令(替换成你的RPC用户名和密码),测试能否正常调用钱包接口:
curl --user 你的RPC用户名:你的RPC密码 http://127.0.0.1:8332/wallet/test2 -d '{"jsonrpc": "1.0", "id": "test", "method": "getwalletinfo", "params": []}'
如果返回包含钱包余额、地址数量的JSON数据,说明RPC连接正常;如果报错,检查端口是否正确、Bitcoin Core是否在运行,或者rpcallowip是否允许本地访问。
4. 不要尝试直接读取wallet.dat
Bitcoin Core的wallet.dat是加密的数据库文件(即使你没设置钱包密码,也有默认加密逻辑),无法通过直接指定文件路径让bitcoinlib加载。必须通过Bitcoin Core提供的RPC接口来操作钱包,由Core负责处理钱包文件的读写。
内容的提问来源于stack exchange,提问作者MylesIsHere
相关产品推荐
相关产品推荐

