use*_*608 0 python text file sl4a
我需要打开一个文件,读取一行,对其进行哈希处理,然后保存到其他文件中。我应该在脚本的开头打开两个文本文件,还是每次保存/读取时都打开?我是所有这一切的新手,并且我正在将python for sl4a用于android。到目前为止,这是我的代码:
import android
import hashlib
import time
name = 0
droid = android.Android()
name = raw_input("Enter a password to hash: ")
hash_object = hashlib.md5 (name)
print(hash_object.hexdigest())
time.sleep(2)
print name
f = open('name.txt', 'w',)
f.write(hash_object.hexdigest())
f.close()
Run Code Online (Sandbox Code Playgroud)
如果要读取文件name.txt并写入另一个文件:
with open('name.txt', 'r') as f, open('out.txt', 'w') as f1:
line = f.next() # get first line
hash_object = hashlib.md5 (line)
f1.write(hash_object.hexdigest()) # write to second file
Run Code Online (Sandbox Code Playgroud)