<分区>
我卡住了。我需要使用 Python 编写代码来按大小查找文件并将其名称和大小添加到列表中。我有一个按名称在目录中搜索文件的程序。我需要使用 get opt 制作另一个标志以按大小进行搜索。
import getopt
import sys
import os
from os import listdir, walk
from os.path import isfile, join
def find_by_name(name, path, result): #Define a function to search the file by it's name
result = []
for root, dirs, files in os.walk(path):
if name in files:
result.append(os.path.join(name)) #Join the file to the list called result
else:
print ("Nothing was found by %s" % name)
return result
def main():
path_dir = raw_input("Select the directory you want to search: ")
results = []
try:
opts, args = getopt.getopt(sys.argv[1:], 'n:y:d:')
except getopt.GetoptError as err:
print (err)
sys.exit
for o, a in opts:
if o in ("-n", "--name"):
pro = find_by_name(a, path_dir, results)
if __name__ == "__main__":
main()