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()