0

Now I am rewriting Python2 scripts to Python3 scripts. In the Python2 script, the code below was successfully launched. But in the Python3 script, it had an syntax error. If somebody knows why it happens, please tell me why and how can I fix it.

hash = []
for root, dirs, files in os.walk(directory):
    for names in files:
        if not names.endswith( '.py' ):
            continue
        filepath = os.path.join(root,names)
        try:
            f1 = open(filepath, 'rb')
        except:
            # You can't open the file for some reason
            f1.close()
            continue
        hash.append( (names, hashlib.sha256(f1.read()).hexdigest() ) )

hash.sort( key=lambda tup: tup[ 1 ])
digest = reduce( lambda d, (x,y): d + '\n' + y, hash, '')
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Crown716
  • 115
  • 10
  • Can you provide a working example with some inputs and outputs? – zedfoxus Nov 05 '21 at 16:42
  • Looks like you could just write `digest = ''.join('\n' + y for x, y in hash)`. Or `'\n'.join(y for x, y in hash)` if you don't care about the first line break. – mkrieger1 Nov 05 '21 at 16:47
  • 2
    "I tried all of them" - did you also try [`lambda d, xy: d + '\n' + xy[1]`](https://stackoverflow.com/a/21893097)? – mkrieger1 Nov 05 '21 at 16:50
  • Yes, I tried. But it didn't work. Please tell me another solution. – Crown716 Nov 05 '21 at 16:51
  • What do you mean by "it didn't work"? I already told you another solution in my earlier comment. And there are about 10 other solutions in the linked Q&A. I'm sure you didn't try them all in the last 5 minutes. – mkrieger1 Nov 05 '21 at 16:52
  • 1
    lambda d, xy: d + '\n' + xy[1] worked for me. I misunderstand. Really thank you. – Crown716 Nov 05 '21 at 17:04

0 Answers0