python 常用功能锦集

环境:linux

编程语言:python

版本:Python 2.6.6 (r266:84292, Apr 11 2011, 15:52:27)

 

(1)如何获取指定文件的属主:

 

def getowner(path2):

import os

import pwd

return pwd.getpwuid(os.stat(path2).st_uid).pw_name

 

 

(2)如何递归列出指定目录的所有file(不包括目录):

def listfiles(path2):

        import os

        tmp=os.walk(path2)

        full_files=[]

        for root,dirs, files in tmp:

                for file in files:

                        full_files.append(os.path.join(root,file))

        return full_files

 

 

(3)如何获取指定文件的权限,如755

def get_power(path3):
'''
path3 is directory or regular file
'''
	import os
	return oct(os.stat(path3)[0])[-3:]
 

 

(4)如何设置权限

(类似于 chmod 755 /home/user2)

def chmod(path4,str_power):
	import os
	if not os.path.exist(path4):
		return 4 #file does not exist
	os.chmod(path4,int(str_power,8))
 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值