博主信息
博文 41
粉丝 0
评论 1
访问量 48496
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
Python高效编程技巧实战(9)
yeyiluLAMP
原创
1050人浏览过

snipaste20170917_220116.png

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


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学