设为首页 收藏本站
查看: 845|回复: 0

[经验分享] python 文件系统

[复制链接]
发表于 2017-4-26 09:48:18 | 显示全部楼层 |阅读模式
文件处理
mkfifo()/mknod()
a
创建命名管道/创建文件系统节点
remove()/unlink()  Delete file 删除文件
rename()/renames()
b
重命名文件
*stat
c
() 返回文件信息
symlink() 创建符号链接
utime()  更新时间戳
tmpfile() 创建并打开('w+b')一个新的临时文件
walk()
a
生成一个目录树下的所有文件名  目录/文件夹
chdir()/fchdir()
a
改变当前工作目录/通过一个文件描述符改变当前工作目录
chroot()
d
改变当前进程的根目录
listdir()  列出指定目录的文件
getcwd()/getcwdu()
a
返回当前工作目录/功能相同, 但返回一个 Unicode 对象
mkdir()/makedirs() 创建目录/创建多层目录
rmdir()/removedirs()  删除目录/删除多层目录
访问/权限
access() 检验权限模式
chmod()   改变权限模式
chown()/lchown()
a
改变 owner 和 group ID/功能相同, 但不会跟踪链接
umask()  设置默认权限模式
文件描述符操作
open()  底层的操作系统 open (对于文件, 使用标准的内建 open() 函数)
read()/write()  根据文件描述符读取/写入数据
dup()/dup2()  复制文件描述符号/功能相同, 但是是复制到另一个文件描述符
设备号
makedev()
a
从 major 和 minor 设备号创建一个原始设备号
Edit By Vheavens
Edit By Vheavens
major()
a
/minor()
a
从原始设备号获得 major/minor 设备号


os.path 模块中的路径名访问函数
basename()  去掉目录路径, 返回文件名
dirname()  去掉文件名, 返回目录路径
join()   将分离的各部分组合成一个路径名
split()   返回 (dirname(), basename()) 元组
splitdrive()  返回 (drivename, pathname) 元组
splitext()  返回 (filename, extension) 元组
信息
getatime()   返回最近访问时间
getctime()   返回文件创建时间
getmtime()   返回最近文件修改时间
getsize()   返回文件大小(以字节为单位)

查询
exists()   指定路径(文件或目录)是否存在
isabs()   指定路径是否为绝对路径
isdir()   指定路径是否存在且为一个目录
isfile()  指定路径是否存在且为一个文件
islink()  指定路径是否存在且为一个符号链接
ismount()   指定路径是否存在且为一个挂载点
samefile()   两个路径名是否指向同个文件




# -*- coding: cp936 -*-
import os
tmpdir='C:\\temp'
if os.path.isdir(tmpdir):
print 'is dir'
else:
print 'not temp directory available'
#tmpdir=''
if tmpdir:
os.chdir(tmpdir)
#跳转到tmpdir目录下
cwd=os.getcwd()
#获取当前路径
print '***current temporary directory'
print cwd
print '***creating example directory...'
os.mkdir('example')
#创建文件夹
os.chdir('example')
cwd=os.getcwd()
print '**** new  working directroy'
print cwd
print '*** original directory listing'
print os.listdir(cwd)#列出cwd下所有目录和文件
print '*****creating test file...'
fobj=open('test','w')
fobj.write('foo\n')
fobj.write('bar\n')
fobj.close()
print '***updated directory listing'
print os.listdir(cwd)
#列出cwd下所有目录和文件
print """***renaming 'test' to 'filetest'"""
os.rename('test','filetest.txt')
print '****updated directory listing:'
print os.listdir(cwd)
path=os.path.join(cwd,os.listdir(cwd)[0])
print '*** full file pathname'
print path
print '**(pathname,basename)=='
print os.path.split(path)
#分割文件名和路径(事实上,如果你完全使用目录也会将uohou一个目录昨晚文件名而分离,
#同时他不会判断文件或目录是否存在
print '***(filename,extension)=='
print os.path.splitext(os.path.basename(path))
#os.path.splitext()分离文件名与扩展名
#os.path.basename() 返回文件名
print '*** displaying file contents:'
fobj=open(path)
#path=c:\\temp\\example\\filetest.txt
'''
循环filetest.txt
'''
for eachLine in fobj:
print eachLine,
fobj.close()
print '***deleting test file'
print 'path',path
os.remove(path)
#删除filetest。txt文件
print '***updated directory listing:'
print cwd
print os.listdir(cwd)
print 'os.pardir',os.pardir
os.chdir(os.pardir)
#返回上级目录temp
print '***deleting test directory'
os.rmdir('example')
#删除example文件夹
print '***DONE'


运行结果:
>>> ================================ RESTART ================================
>>>
is dir
***current temporary directory
C:\temp
***creating example directory...
**** new  working directroy
C:\temp\example
*** original directory listing
[]
*****creating test file...
***updated directory listing
['test']
***renaming 'test' to 'filetest'
****updated directory listing:
['filetest.txt']
*** full file pathname
C:\temp\example\filetest.txt
**(pathname,basename)==
('C:\\temp\\example', 'filetest.txt')
***(filename,extension)==
('filetest', '.txt')
*** displaying file contents:
foo
bar
***deleting test file
path C:\temp\example\filetest.txt
***updated directory listing:
C:\temp\example
[]
os.pardir ..
***deleting test directory
***DONE
>>>


===========================================================================
统计文件中行数
import os
tmp='c:\\python27\\1.py'
lines=0
objf=open(tmp)
for ff in objf:
print ff
lines+= ff.count('\n')
print 'lines=%d'%lines

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-369361-1-1.html 上篇帖子: python读写xls 下篇帖子: python函数定义
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表