In [39]: ll total 0 -rw-rw-r--+ 1 yeyilu 0 Sep 17 06:42 a.sh -rw-rw-r--+ 1 yeyilu 0 Sep 17 06:42 b.py -rw-rw-r--+ 1 yeyilu 0 Sep 17 06:42 c.h -rw-rw-r--+ 1 yeyilu 0 Sep 17 06:42 d.java -rw-rw-r--+ 1 yeyilu 0 Sep 17 06:42 e.py -rw-rw-r--+ 1 yeyilu 0 Sep 17 06:43 f.cpp -rw-rw-r--+ 1 yeyilu 0 Sep 17 06:43 g.sh -rw-rw-r--+ 1 yeyilu 0 Sep 17 06:43 h.c In [42]: import os, stat In [43]: os.listdir('.') Out[43]: ['g.sh', 'e.py', 'c.h', 'f.cpp', 'd.java', 'h.c', 'a.sh', 'b.py'] In [45]: s = 'g.sh' In [46]: s.endswith('.sh') Out[46]: True In [47]: s.endswith('.py') Out[47]: False In [48]: s.endswith(('.sh','.py')) Out[48]: True In [49]: s.endswith? Type: builtin_function_or_method String form: <built-in method endswith of str object at 0xb6041c40> Docstring: S.endswith(suffix[, start[, end]]) -> bool Return True if S ends with the specified suffix, False otherwise. With optional start, test S beginning at that position. With optional end, stop comparing S at that position. suffix can also be a tuple of strings to try. p = ('a.py', 'b.sh', 'c.java', 'c.h', 'd.cpp', 'g.h','1.sh') t = ('a.py', 'b.sh', 'c.java', 'c.h', 'd.cpp', 'g.h','test.java') In [92]: def filter_ord(*y): res = [] for x in y: if x.endswith(('.java','.sh')): res.append(x) return res ....: In [94]: filter_ord(*t,*p) Out[94]: ['b.sh', 'c.java', 'test.java', 'b.sh', 'c.java', '1.sh'] 方法一: In [112]: res = [] In [113]: for x in os.listdir('.'): .....: if x.endswith(('.sh','py')): .....: res.append(x) .....: In [114]: res Out[114]: ['g.sh', 'e.py', 'a.sh', 'b.py'] 方法二: In [53]: list(filter(lambda x:x.endswith(('.sh','.py')),os.listdir('.'))) Out[53]: ['g.sh', 'e.py', 'a.sh', 'b.py'] 方法三: In [54]: [name for name in os.listdir('.') if name.endswith(('.sh','.py'))] Out[54]: ['g.sh', 'e.py', 'a.sh', 'b.py'] In [55]: os.stat('e.py') Out[55]: os.stat_result(st_mode=33204, st_ino=277206, st_dev=2049, st_nlink=1, st_uid=1000, st_gid=1000, st_size=0, st_atime=1505655776, st_mtime=1505655776, st_ctime=1505655776) In [56]: os.stat('e.py').st_mode Out[56]: 33204 In [57]: oct? Type: builtin_function_or_method String form: <built-in function oct> Namespace: Python builtin Definition: oct(number) Docstring: Return the octal representation of an integer. >>> oct(342391) '0o1234567' In [58]: oct(os.stat('e.py').st_mode) Out[58]: '0o100664' In [64]: os.chmod('e.py',os.stat('e.py').st_mode | stat.S_IXUSR) In [65]: ll total 0 -rw-rw-r--+ 1 yeyilu 0 Sep 17 06:42 a.sh -rw-rw-r--+ 1 yeyilu 0 Sep 17 06:42 b.py -rw-rw-r--+ 1 yeyilu 0 Sep 17 06:42 c.h -rw-rw-r--+ 1 yeyilu 0 Sep 17 06:42 d.java -rwxrw-r--+ 1 yeyilu 0 Sep 17 06:42 e.py* -rw-rw-r--+ 1 yeyilu 0 Sep 17 06:43 f.cpp -rw-rw-r--+ 1 yeyilu 0 Sep 17 06:43 g.sh -rw-rw-r--+ 1 yeyilu 0 Sep 17 06:43 h.c