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 from os import listdir | |
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 files = [ f for f in listdir(options.directory) if pattern.match(f)] | |
31 out = file(options.output, 'w') | |
32 with out: | |
jbroman
2017/03/27 19:01:02
super-nit: might as well combine this with the pre
| |
33 for f in files: | |
34 out.write(f + '\n') | |
35 | |
36 if __name__ == '__main__': | |
37 sys.exit(main()) | |
OLD | NEW |