Python 清洗USB驱动器的递归程序
几天前,一台受感染的计算机用一种快捷方式病毒感染了我的USB驱动器。我的USB驱动器里有很多软件,我把它插入我的Linux机器,清理了很多文件,但病毒所做的是在每个文件夹中创建了一个带有文件夹名称的.exe文件。每个.exe文件都可能感染另一台电脑。因此,我花了大量时间试图制作一个python脚本,用于每个文件夹检查是否存在具有该文件夹名称的.exe文件。删除该文件并递归地为每个子文件夹运行该函数。但我到目前为止所做的一切在这里都不起作用Python 清洗USB驱动器的递归程序,python,python-2.7,recursion,directory,Python,Python 2.7,Recursion,Directory,几天前,一台受感染的计算机用一种快捷方式病毒感染了我的USB驱动器。我的USB驱动器里有很多软件,我把它插入我的Linux机器,清理了很多文件,但病毒所做的是在每个文件夹中创建了一个带有文件夹名称的.exe文件。每个.exe文件都可能感染另一台电脑。因此,我花了大量时间试图制作一个python脚本,用于每个文件夹检查是否存在具有该文件夹名称的.exe文件。删除该文件并递归地为每个子文件夹运行该函数。但我到目前为止所做的一切在这里都不起作用 #!/usr/bin/python from
#!/usr/bin/python
from __future__ import print_function
import os
import argparse
def deldir(fol):
# changing to the directory
os.chdir(fol)
#Trivial case delte the virus in the root folder
cwd = os.getcwd()
#getting just the name of the folder rather than full path by splitting from "/"
cwd = str(cwd).split('/')[-1]
#if any .exe file exists with that name
if any(str(cwd + '.exe') in s for s in os.listdir('.')):
# delete that file
os.remove(str(cwd + '.exe'))
print('Deleting virus file')
#making a list with all the contents in the directory
dirlist = os.listdir('.')
print(dirlist)
#Looping through the directory that we just made a list of its contents
for i in dirlist:
#checking if it is a directory or not
if os.path.isdir(i):
#changing dir to that directory
os.chdir(i)
#getting the current working directory
cwd = os.getcwd()
#getting just the name of the folder rather than full path by splitting from "/"
cwd = str(cwd).split('/')[-1]
#if any .exe file exists with that name
if any(str(cwd + '.exe') in s for s in os.listdir('.')):
# delete that file
os.remove(str(cwd + '.exe'))
print('Deleting virus file', str(cwd + '.exe'))
#listing filders in current directory
for j in os.listdir('.'):
#passing that subdirectory to the function itself if its a folder
if os.path.isdir(j):
deldir(j)
else:
print('Not a folder so skipping')
def main():
#parsing the command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("fol", help="Directory to enter")
args = parser.parse_args()
#passing the command line argument with the folder path to "deldir" function
deldir(args.fol)
if __name__ == "__main__":
main()
请给我指出正确的方向,我不知道我做错了什么感谢您阅读这里是我在您的代码中看到的错误 一,
os.getcwd
->os.getcwd()
这是一个函数
二,。
您可以在for循环中使用os.chdir(i)。这将仅适用于该目录中的第一个目录
三,。
该算法是错误的,它没有检查目录级别1,3,5,。。。对于病毒
正如Hiro和MYGz的评论。对于递归目录,os.walk/os.scandir是更简单的方法。但是你的方式更适合于练习和学习编程。我用操作系统按我想要的方式完成了它。walk()这种方式非常简单,而且失去了乐趣!:'(.我希望修复我自己的代码,但有些不对劲。不管怎样,如果有人遇到这种类型的病毒或其他东西,这里是工作代码
#!/usr/bin/python
from __future__ import print_function
import os
import argparse
def deldir(fol):
for root, dirs, files in os.walk(fol):
try:
os.remove(str(root + '/' + str(root).split('/')[-1 ] + '.exe'))
except:
print('Does not exists' ,str(root + '/' + str(root).split('/')[-1 ] + '.exe'))
def main():
#parsing the command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("fol", help="Directory to enter")
args = parser.parse_args()
#passing the command line argument with the folder path to "deldir" function
deldir(args.fol)
if __name__ == "__main__":
main()
如果您想递归访问目录树,将有帮助。@python3.5中的hiroprotation
os.scandir()
更快。您可以将其作为3.5之前版本的软件包安装。os.getcwd()
not justos.getcwd
如果您想查看USB中的所有目录,可以参考my。然后您可以获取pwd名称os.getcwd().split(“/”[-1])并删除.exe文件。