python - 将多个 txt 文件中的行读入变量
问题描述
我在一个目录中有多个 txt 文件,其中每个 txt 文件都有几行哈希:
546516fdsfgbfdgbfdf
232321dfsfdsgfdgvfd
321656fdsfsffgfdgfd
我正在尝试读取目录中所有 txt 文件中的每一行,然后将它们传递给“ hash ”变量并每次重新运行脚本以下载哈希二进制文件。
import requests
f = open('*.txt')
hash = f.read().splitlines()
f.close()
params = {'apikey': 'XXXXXXXXXXXXXX', 'file': (hash)}
response = requests.get('https://www.test.com/file/download', params=params)
downloaded_file = response.content
if response.status_code == 200:
with open('/tmp/sample.bin', 'wb') as f:
f.write(response.content)
解决方案
如果您只是想将整行读入 num,那么就可以了。
if response.status_code == 200:
with open('/tmp/file.bin', 'wb') as f:
num = f.read()
推荐阅读
- node.js - 通过 express-validator 验证用户
- function - 哈斯克尔。将名称分配给值的阈值
- c - c 的 sprintf 改变了我的计数器
- python-3.x - 无法通过 Visual Studio 2017 15.7.1 安装 pygraphviz
- android - 重复资源:有没有办法在android studio中构建时自动忽略重复资源
- c - 使用 +/-1 优化整数乘法
- node.js - 无法获取/错误
- javascript - 如何在 Jest 测试之前配置 beforeEach 函数?
- xcode - 尝试使用相关项目在沙盒应用程序中创建备份文件失败
- sockets - 如何通过云网关提供多种服务?