首页 > 解决方案 > 将多个 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)

标签: python

解决方案


如果您只是想将整行读入 num,那么就可以了。

if response.status_code == 200:
    with open('/tmp/file.bin', 'wb') as f:
        num = f.read()

推荐阅读