Python Forum

Full Version: io.UnsupportedOperation: not readable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i dont understand why i get this error,
thnks for your helping
import hashlib
import csv
input_file_name = 'c:/users/shervin/documents/pass.csv'
output_file_name = 'c:/users/shervin/documents/pass1.csv'

def hash_password_hack(input_file_name, output_file_name):
    source = []
    source_names = []
    with open(input_file_name,'r', newline='') as f, open(output_file_name, 'w',newline='') as f:
        reader = csv.reader(f)
        for row in reader:
            source.append(row[1])
            source_names.append(row[0])

    pass_hash = {}
    for i in range(1000, 10000):
        i_st = str(i)
        i_st = hashlib.sha256(i_st.encode("utf-8"))
        hashed = i_st.hexdigest()
        pass_hash[hashed] = i

    for h in range(0, len(source)):
        if source[h] in pass_hash:
            csv_output.writerow(source_names[h], pass_hash[source[h]])

hash_password_hack(input_file_name, output_file_name)
Output:
Traceback (most recent call last): File "C:/Users/shervin/Downloads/15233519514032056 (1).py", line 26, in <module> hash_password_hack(input_file_name, output_file_name) File "C:/Users/shervin/Downloads/15233519514032056 (1).py", line 11, in hash_password_hack for row in reader: io.UnsupportedOperation: not readable
In your with statement on line 9, you have assigned both the input and output versions of your file to 'f'. So the output one overwrites the input one, and you can't read from a file open in 'w' mode. Since you are not writing to the output in that with block, I would just remove that second file assignment.