I have a problem with my algorithm apparently it skips a lot of sha1 hashes
when executing.
No problem with the filename, but im having problem with having this output:
filename
+ sha1
\\n
For every each of them. I can guess it`s because of os.walk in some way but im not that expert ATM.
txt = open('list','w')
for dirpath, dirnames, filenames in os.walk(dir_path):
text = str(filenames)
for tag in ("[", "]", " ","'"):
text = text.replace(tag, '')
text = str(text.replace(',','\n'))
for i in filenames:
m = hashlib.sha1(str(text).encode('utf-8')).hexdigest()
txt.write(text+" "+str(m)+"\n")
txt = txt.close()
Thanks
Change:
txt = open('list','w')
to:
txt = open('list','a')
You are using "w" which overwrites any previous content. You need "a", which appends to the existing file without overwriting.
What looks like a potential issue is that you are converting filenames
, which is a list of each individual file in the current folder, into a string and then performing replacements on that list. I assume what you intended to do instead was replace within each filename string those special tags
. Try the below.
txt = open('list','w')
for dirpath, dirnames, filenames in os.walk(dir_path):
for text in filenames:
text = re.sub('[\[\]," "]',"",text)
m = hashlib.sha1(str(text).encode('utf-8')).hexdigest()
txt.write(text+" "+str(m)+"\n")
txt = txt.close()
As requested, if you dont' want to use re
, just do what you had originally done:
text = 'fjkla[] k,, a,[,]dd,]'
for badchar in '[]," "]':
text = text.replace(badchar,"")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.