一. os模块
1.os.access()
查看文件是否有指定权限,结果为True或False
os.access(path, mode)
path:指定文件路径
mode:判定依据 (F_OK(是否存在),R_OF(是否可读),W_OK(是否可写),X_OK(是否可执行))
>>> os.access('/python/a.py',os.F_OK) #是否存在
True
>>> os.access('/python/a.py',os.R_OK) #是否可读
True
>>> os.access('/python/a.py',os.W_OK) #是否可写
True
>>> os.access('/python/a.py',os.X_OK) #是否可执行
False
2.os.chdir()
用于更改当前工作目录到指定的路径
>>> os.getcwd() #查看当前工作路径
'/python
>>> os.chdir('/sulong/test/') #修改当前工作路径到‘/sulong/test/’
>>> os.getcwd()
'/sulong/test'
3.os其他常用命令
>>> os.name #判断现在的实用平台,windows返回‘nt’,linux返回
'posix'
>>> os.getcwd() #返回当前工作的目录
'/root'
>>> os.listdir('.') #返回指定目录下所有的文件和目录名
['file.txt', 'test', 'caidan.py', 'test.txt', 'test.py', 'test1.py', 'enumerate.py', 'login.py']
>>> os.remove('test1.py') #删除指定文件
>>> os.listdir('.')
['file.txt', 'test', 'caidan.py', 'test.txt', 'test.py', 'enumerate.py', 'login.py']
>>> os.rmdir('aaa') #删除指定目录
>>> os.mkdir('directory') #创建目录,只能创建一层目录
>>>os.path.isfile()——判断指定对象是否为文件。是返回True,否则False
>>> os.path.isfile('test.py') #为文件返回True
True
>>> os.path.isfile('directory') #此为目录则返回false
False
>>> os.path.isdir('directory') #判断指定对象是否为目录。
True
>>> os.path.exists('/python/test.py') #判断指定的对象是否存在
True
>>> os.path.exists('/python/caidan')
False
>>> os.path.split('/python/test.py') #返回路径的目录和文件名
('/python', 'test.py')
>>> os.getcwd() #获取当前工作的目录
'/python'
>>> os.system('pwd') #执行shell命令
/python
>>> os.system("echo 'hello world!'")
hello world!
>>> os.path.getsize('directory') #获得文件的大小,如果为目录返回0
>>> os.path.abspath('.') #获得绝对路径
'/python'
>>> os.path.join('/python/directory/','test.py') #链接目录和文件名
'/python/directory/test.py'
>>> os.path.basename('/python/directory/test.py') #返回文件名
'test.py'
>>> os.path.basename('/python/directory')
'directory'
>>> os.path.dirname('/root/directory/test.py') #返回文件路径
'/root/directory'
>>> os.path.getmtime('.') #返回在此path下最后一次修改的时间戳
1510553280.2887046