0

I'm trying to create a function that will be used as part of a larger script. I want this function to hash all the files in the current working directory and output the results to that same directory as a text file. This is what I've come up with:

def input_hash (in_file):
    print("All the input files have been hashed using the sha1 checksum \n")
    cwd = os.getcwd()                   
    for video in os.listdir(cwd):       
        with open(video, 'rb') as getsha1:
            data = getsha1.read()
        checksum = hashlib.sha1(data).hexdigest()
        filenames = os.path.join(video, checksum)
        print(filenames)                    #returns filenames as str
        output = open("Input_hash.txt", "w+")
        output.writelines([i + '\n' for i in filenames])
        output.close()

When I run this function on my test files the "filenames" variable has the following information stored in it and each item is a string:

All the input files have been hashed using the sha1 checksum

IMG_0020.MOV\35bcfab1df29bc20113420683a81982f0c731012 IMG_0063.MOV\251eb1376d6d2fff047244192c4cdaf88b251e92 IMG_0065.MOV\2cce5734c72b00e608ff511dafabf953167bc5c5 IMG_0074.MOV\4bcada3d839e06a477ecd3b1eba8a75ce21e8e0f IMG_0076.MOV\46d7d619780f12eb22c088ff0da505f10ecaa92f IMG_0087.MOV\b89fbc6a1a949ff7f13ac0ac09d10518c3961abe IMG_0113.MOV\0e42993b3cd8261ac869cd8947157e5936fb52dd IMG_0120.MOV\c9c0eea3b4c00eeca1a1cb218564a01db2ae7ed9 IMG_0242.MOV\82d8bb859396b0b70c4cb1ff265daa8a82db2143 IMG_0247.MOV\d2a9291ca26df3f03cbae5ec115f08f6084a906a

Which is exactly what I want; however, the only output written to the txt file is the last line in that variable and it's written vertically, not horizontally.

So, I'm trying to write all the contents of the "filenames" variable to a text exactly as it appears here and, I've hit a brick wall.

I've tried all these solutions Similar Question

None seemed to work. Greatly appreciate any advice or assistance.

@icebreaker454

Am I formatting this incorrectly?

def input_hash (in_file):
print("All the input files have been hashed using the sha1 checksum ")
cwd = os.getcwd()                   #gets current directory
for video in os.listdir(cwd):       #gets all the files in the directory
    with open(video, 'rb') as getsha1:
        data = getsha1.read()
    checksum = hashlib.sha1(data).hexdigest()
    filenames = []
    for video in os.listdir(cwd):
        filenames.append(os.path.join(video, checksum))
    with open("output.txt", "w+") as output:
        output.writelines([name + "\n" for name in filenames])
    output.close()

Solution:

def input_hash (in_file):
    print("All the input files have been hashed using the sha1 checksum ")
    cwd = os.getcwd()                  
    filenames = []
    for video in os.listdir(cwd):
        with open(video, 'rb') as getsha1:
            data = getsha1.read()
        checksum = hashlib.sha1(data).hexdigest()
        filenames.append(os.path.join(video, checksum))
    with open("output.txt", "w+") as output:
        output.writelines([name + "\n" for name in filenames])
    output.close()
RTGho
  • 3
  • 3

1 Answers1

1

The reason is really the fact that you try to write out your LAST filename + hash to the file. In your script you always assign the current filename path + hashsum to the filenames variable. In the end, you have it set to the last filename + hashsum.

Since your last filename is a string, it could be iterated, without an error:

output.writelines([i + '\n' for i in filenames])

So, having your last filename = "IMG_0247.MOV\d2a9291ca26df3f03cbae5ec115f08f6084a906a", the [i + '\n' for i in filenames] would become ["I", "\n", "M", "\n", "G", ...], and that's exactly the reason you see it written vertically

To fix this, you should simply append each of the filenames to an array, and write it out in the end

filenames = []
for video in os.listdir(cwd):
   with open(video, 'rb') as getsha1:
       data = getsha1.read()
   checksum = hashlib.sha1(data).hexdigest()
   filenames.append(os.path.join(video, checksum))

with open("output.txt", "w+") as output:
    output.writelines([name + "\n" for name in filenames])
Icebreaker454
  • 1,031
  • 7
  • 12
  • Hmm, ok I'm closer than I was. What that does is join the checksum of that last file and appends it to every filename in the list. So the checksum for IMG_247 is appended to all the other filenames in the list........BUT... It does list the filenames correctly now. – RTGho Feb 24 '21 at 19:07
  • @RTGho, make sure you have only one `for video in os.listdir(cwd)` loop. I edited the answer – Icebreaker454 Feb 25 '21 at 16:44
  • Ah! Thank you so much.........greatly, greatly appreciate the clarification! Problem solved. – RTGho Feb 25 '21 at 17:32