1
\$\begingroup\$

The purpose is just to create a dictionary with a path name, along with the file's SHA-256 hash.

I'm very new to Python, and have a feeling that there's a much better way to implement the following code. It works, I'd just like to clean it up a bit. fnamelst = [r'C:\file1.txt', r'C:\file2.txt']

[fname.replace('\\', '\\\\') for fname in fnamelst]
diction = [{fname: hashlib.sha256(open(fname, 'rb').read()).digest()} for fname in fnamelst]
for iter in range(0,len(fnamelst)):
 dic2 = {fnamelst[iter]: hashlib.sha256(open(fnamelst[iter], 'rb').read()).digest()}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jun 3, 2013 at 14:18
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Why are you replacing slashes?? \$\endgroup\$ Commented Jun 3, 2013 at 14:20
  • 1
    \$\begingroup\$ Well, you've created the dictionary twice. What's the question? \$\endgroup\$ Commented Jun 3, 2013 at 14:20
  • 2
    \$\begingroup\$ Note that the for loop will overwrite dic2 each time, instead of extending it. \$\endgroup\$ Commented Jun 3, 2013 at 14:24

3 Answers 3

4
\$\begingroup\$

In your code, you show two ways to create your dictionary, but both are somewhat flawed:

  • diction will be a list of dictionaries, each holding just one entry
  • dic2 will get overwritten in each iteration of the loop, and will finally hold only one dictionary, again with only one element.

Instead, try this:

diction = dict([ (fname, hashlib.sha256(open(fname, 'rb').read()).digest())
 for fname in fnamelst ])

Or shorter, for Python 2.7 or later:

diction = { fname: hashlib.sha256(open(fname, 'rb').read()).digest()
 for fname in fnamelst }

The first one uses a list comprehension to create a list of key-value tuples, and then uses the dict function to create a single dictionary from that list. The second one does the same, using a dict comprehension.

answered Jun 3, 2013 at 14:38
\$\endgroup\$
0
2
\$\begingroup\$

In python, for iter in range(0,len(container)): is pretty much always a bad pattern.

Here, you can rewrite :

for f in fnamelst:
 dic2 = {f: hashlib.sha256(open(f, 'rb').read()).digest()}

As @tobias_k pointed out, this doesn't do much at the moment as dict2 gets overriden. In Python 2.7+ or 3, you can just use the dict comprehension directly.

answered Jun 3, 2013 at 14:30
\$\endgroup\$
1
\$\begingroup\$

You have some confusion about using backslahses in Python strings.

r'C:\file1.txt' is a raw string and the backslash is a backslash. r in front denotes raw.

'C:\\file1.txt' is the same string written as a normal string literal. Now the backslash has to be written as \\, because otherwise \f would be interpreted as a control character \x0c.

Doubling the backslash using replace serves no purpose here. Moreover, as tobias_k points out, the line with the replace has no effect at all because the resulting modified list is not assigned to a variable.

answered Jun 4, 2013 at 6:01
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Moreover, the line with the replace has no effect at all, as the strings in the list are immutable, and the new list is not assigned to a variable. \$\endgroup\$ Commented Jun 4, 2013 at 7:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.