OLD | NEW |
(Empty) | |
| 1 # Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 """Generates a list of all files in a directory. |
| 5 |
| 6 This script takes in a directory and an output file name as input. |
| 7 It then reads the directory and creates a list of all file names |
| 8 in that directory. The list is written to the output file. |
| 9 There is also an option to pass in '-p' or '--pattern' |
| 10 which will check each file name against a regular expression |
| 11 pattern that is passed in. Only files which match the regex |
| 12 will be written to the list. |
| 13 """ |
| 14 |
| 15 from optparse import OptionParser |
| 16 import os |
| 17 import re |
| 18 import sys |
| 19 |
| 20 def main(): |
| 21 parser = OptionParser() |
| 22 parser.add_option('-d', '--directory', help='Read files from DIRECTORY') |
| 23 parser.add_option('-o', '--output', help='Write list to FILE') |
| 24 parser.add_option('-p', |
| 25 '--pattern', |
| 26 help='Only reads files that name matches PATTERN', |
| 27 default=".") |
| 28 (options, _) = parser.parse_args() |
| 29 pattern = re.compile(options.pattern) |
| 30 print(options.directory) |
| 31 print(os.listdir(options.directory)) |
| 32 files = [ f for f in os.listdir(options.directory) if pattern.match(f)] |
| 33 print(files) |
| 34 print(os.getcwd()) |
| 35 print(options.output) |
| 36 with file(options.output, 'w') as out: |
| 37 for f in files: |
| 38 out.write(f + '\n') |
| 39 |
| 40 if __name__ == '__main__': |
| 41 sys.exit(main()) |
OLD | NEW |