0

I have multiple txt files in a directory where each txt file have few lines of hashes:

546516fdsfgbfdgbfdf

232321dfsfdsgfdgvfd

321656fdsfsffgfdgfd

I'm trying to read each line in all the txt files in the directory and then pass them to the "hash" variable And run the script every time anew in order to download the hash binary file.

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)
bugnet17
  • 173
  • 1
  • 1
  • 12

3 Answers3

0

If you're trying to just read the whole line into num, that will do the trick.

if response.status_code == 200:
    with open('/tmp/file.bin', 'wb') as f:
        num = f.read()
luke
  • 11
  • 2
  • OP wants to write output into the `file.bin`, as I see it, after making a request. You're reading from it, which doesn't solve the problem. – darthbhyrava Nov 05 '18 at 08:52
  • As i was writing this answer, OP just wanted to write the content of file.bin to the variable num. – luke Nov 05 '18 at 10:19
0

Had a similar problem, I needed to change a certain String in every file of a folder if appearing.

Important This is a rather older script, so there are some to do's. I would recommend to resolve those issues as an exercise to improve the code quality:

  • Using a try and raise Block if an Error occurs.
  • Define regular expressions object instead of defining them in the line, the algorithm works faster with this
  • Applying List Comprehensions

Note I use regex101.com for creating my regular expressions.

import os, fnmatch, re

allLinesString = []

def findReplace(directory, filePattern):
    for path, dirs, files in os.walk(os.path.abspath(directory)):
        for filename in fnmatch.filter(files, filePattern):
            filepath = os.path.join(path, filename)
            global i
                with open(filepath, "r") as f:
                    for line in f:
                    # So lets decide here what to do when we go through each line of each file found in the directory
                        if "String" in line:
                            # Okay and here we write the String if "String" is in line to an array
                            allLinesString.append(line)
                            # And if you want you can even use a regex expression to manipulate the "String" which you just found, or delete certain characters what so ever
                            # Or write the entry to a varible called num in your case
                            allLinesString[i] = re.sub(r'^\s', '', allLinesString[i])

# In this case the function searches for all markdown files for the specific string
if __name__ == '__main__':
    findReplace("wantedDirectoryPath/", "*.md")
JasParkety
  • 131
  • 1
  • 6
0

As I understand it, reading all hashes will solve your problem. Try this, and take a look at the comments, too.

import os
import requests

# If text files aren't in the same dir, edit path as you see fit
path = os.getcwd()

# Reads all text files in current directory.
all_files = [i for i in os.listdir(path) if i[-4:] == '.txt']
all_hashes = []

for file in all_files:
    with open(path+file,'r+') as f:
        lines = f.readlines()
    for line in lines:
        # Handling empty lines only here, add regex or more params to strip if needed
        if not line.strip('\n'):
            continue
        # All hashes are now stored in a list
        all_hashes.append(line.strip('\n'))

# Just iterate over the list and make requests
# You may want to add a delay with time.sleep() in the loop
for hash in all_hashes:
    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:
        # you may want to use 'a' instead of 'wb' here?
        with open('/tmp/sample.bin', 'wb') as f:
            f.write(response.content)

Edit: Also take a look at this post which tells you how you can read all txt files from a directory, and at this one which shows how to read a file, line-by-line, into a list.

darthbhyrava
  • 505
  • 4
  • 14
  • 1
    As a side note, you do not need to close your file if you open it with a `with` statement – BlueSheepToken Nov 05 '18 at 08:53
  • I save all files in the same name: sample.bin. do you know how to save the file in his original name? – bugnet17 Nov 05 '18 at 09:01
  • First, is the above code helping you out? If yes, that's this question solved. Second, saving output by file-name would require you to keep a maintain a record of some sort, maybe a dict: `index_dict = {hash1: filename1, hash2:filename2, ... }`. Then you could use `with open('{0}.bin',format(index_dict[hash1]), 'a+' as f:` before you write your file. – darthbhyrava Nov 05 '18 at 09:07
  • @bugnet17 take a look at this meta [post](https://meta.stackexchange.com/questions/119197/problem-with-users-not-accepting-answers), please. It would be nice for new users to get motivation to answer more posts. Either downvote answers if they don't help when people aren't answering comments for those posts, or upvote/accept if they help. – darthbhyrava Nov 05 '18 at 09:19
  • When running the script got the next error: with open(path+file,'r+') as f: IOError: [Errno 2] No such file or directory: '/tmp/extract-hashestest.txt' – bugnet17 Nov 05 '18 at 09:36
  • Can you post your directory tree, the one with the files, along with the pwd of the script? – darthbhyrava Nov 05 '18 at 09:45
  • root@ubuntu:/tmp/extract-hashes# pwd /tmp/extract-hashes – bugnet17 Nov 05 '18 at 11:55
  • Script and the files located in the same folder: /tmp/extract-hashes – bugnet17 Nov 05 '18 at 12:03
  • Okay, could you copy-paste the output of the command `tree` while in `tmp/extract-hashes`? Also, your error indicates that `'/tmp/extract-hashestest.txt'` doesn't exist, are you sure that you've not forgotten to add a `/` between `extract-hases` and `test.txt` somewhere in your script? Maybe instead of `path + file` in the file opening line, try `path+'/'+file`? – darthbhyrava Nov 05 '18 at 12:08
  • Now is OK but the script sends one HTTP request for all hashes. I need the script will send one HTTP request for each hash in the list. any ideas? – bugnet17 Nov 05 '18 at 12:40
  • The code I've posted above doesn't send one HTTP request for all hashes. It makes exactly _one_ `requests.get()` call for _each_ iteration of the `for hash in all_hashes` loop. That is why I'd suggested using `time.sleep()` so that you don't overload the server you're pinging and maintain API call etiquette. – darthbhyrava Nov 06 '18 at 03:18