Discuss / Python / 第二题

第二题

Topic source

大晨紫Zzz

#1 Created at ... [Delete] [Delete and Lock User]
#!/usr/bin/env python3

import os
import sys

file_list = []
def find_file(filename,filepath):

    files = os.walk(filepath)
    for root,dirs,files in files:
        for file in files:
            if filename in os.path.split(file)[1]:
                res_file = os.path.join(root,file)
                print(res_file)
                file_list.append(res_file)

if __name__ == '__main__':
    if len(sys.argv) == 3:
        file_keys = sys.argv[1]
        file_path = sys.argv[2]
    elif len(sys.argv) == 2:
        file_keys = sys.argv[1]
        file_path = input('Default PATH: /root/helloworld :'.strip())
    elif len(sys.argv) == 1:    
        file_keys = input('Please Input FileName:').strip()
        file_path = input('Default PATH: /root/helloworld :'.strip())
    if os.path.isdir(file_path):
        find_file(file_keys,file_path)
        print('\033[33mThe number of files: {}\033[0m'.format(len(file_list)))
    else:
        sys.exit('\033[31mPATH is not exist,Please retry\033[0m')

  • 1

Reply