python OS模块

初学编程,听从了最多的建议从python学起,早期学习了一些基本操作,照猫画虎写过一点点socket,玩过一点点pygame,但是后来看了一些博客。但是还是感觉只是会了一些皮毛,于是把我之前有所欠缺的部分补回来。这次是把一直犯懒不愿意看、也一直不太明白的os模块抓起来,这也是我的第一篇博客,初来乍到,写的很基础,有的写法可能很不专业,只是作为记录。


1、文件夹操作



(1)建立文件夹(os.dir)


(2)转到当前工作目录(os.chdir)

import os
os.chdir('try_dir')     #转到之前创建的工作目录(相对路径)
cwd = os.getcwd()
print cwd                   
#>>> /home/michael/PythonWorks/python/os/try_dir

(3)重命名文件夹(os.rename)
os.rename('try_dir','try_dir_rename')

(4)列出文件夹下所有文件名(os.listdir)

cwd = os.getcwd()   #不要忘了getcwd
files = os.listdir(cwd)
print files
# >>>['try_dir_rename', 'try_dir', 'try_os.py']
(5)生成目录下所有文件名的所有完整路径(os.walk)

import os
cwd = os.getcwd()
files = os.walk(cwd)
for file in files:
    print file
'''>>> 
('/home/michael/PythonWorks/python/os', ['try_dir_rename', 'try_dir'], ['try_os.py'])
('/home/michael/PythonWorks/python/os/try_dir_rename', [], ['try_file'])
('/home/michael/PythonWorks/python/os/try_dir', [], []) '''
(6)删除目录(rmdir删除目录,rmdirs删除多层目录,区别?)

import os
cwd = os.getcwd()
os.rmdir(cwd +os.sep +'try_dir')
#os模块os.sep -->'/   '   (linux)    ( 在分隔符不同的系统上可能不一样)
#文件被删除

‘’‘附带os模块属性:
os.linesep:文件内 隔行符号
os.sep:分割路径名
os.pathsep:分割文件路径的字符串(干嘛用的?)
os.curdir:当前工作目录的字符串(不可以直接代替getcwd,它返回的是一个   '.')
os.pardir':父目录的字符串('..')
用法:如要在父目录下创建文件:open(os.pardir + os.sep + 'file.txt','w')
*容易忘记 os.sep,注意<pre name="code" class="python">’‘’

 

2、文件操作
(1)创建文件open

(builtin函数,不是os模块中的方法)open一个不存在的文件,把读取模式改成'w'(写),就自动创建一个文件

os.chdir('try_dir_rename')    #改变路径
cwd = os.getcwd()                   
print cwd                                   #/home/michael/PythonWorks/python/os/try_dir_rename
f = open('try_file','w')              #创建了一个名为"try_file"的文件
(2)重命名文件(与文件夹操作相同)

os.rename('try_file','try_file_renamed') 
'''文件被改名了'''
(3)删除文件(remove/unlink)

#先保证自己在对的工作目录,否则需要写完整的工作目录
os.remove('try_file_renamed')

(4)显示文件信息

stat = os.stat('try_file')
print stat                 #有什么比较好的显示文件信息的方法?(list(stat)???)
 '''结果:posix.stat_result(st_mode=33204, st_ino=554750,
                  st_dev=64513L, st_nlink=1, s
                  t_uid=1000, st_gid=1000, st_
                  size=0, st_atime=1420949627,
                  st_mtime=1420949639, st_ctim
                  e=1420949639)'''
'''解释:
st_mode:
st-ino:
st_dev:
st_nlink:
st_dev:
st_nlink:
t_uid:
st_gid:
st_size:文件大小
st_gid:
st_mtime:文件修改时间
st_ctime:文件创建时间
''' 

3、os.path操作

(1)返回路径名,文件名(os.path.basename,os.path.dirname)

cwd = os.getcwd()
loc = cwd + '/par_file.txt'
print loc
file_name = os.path.basename(loc)
print file_name
'''
>>>/home/michael/PythonWorks/python/os
/home/michael/PythonWorks/python/os/par_file.txt
par_file.txt
'''
#dirname则返回pathname
(2)组合路径(os.path.join)

cwd = os.getcwd()
loc = os.path.join(cwd,'par_file.txt')   #注意join方法会自动加上 ’/‘
print loc
’‘’
>>> 
/home/michael/PythonWorks/python/os
/home/michael/PythonWorks/python/os/par_file.txt
‘’‘

(3)返回 basename dirname的元组

loc_tuple = os.path.split(loc)
print loc_tuple
'''
/home/michael/PythonWorks/python/os/par_file.txt
('/home/michael/PythonWorks/python/os', 'par_file.txt')
'''
(4)返回 drivename,pathname的元组(os.path.drivename/os.path.pathname)

*linux下返回的drive 是空字符串

drive_tuple = os.path.splitdrive(loc)
print drive_tuple
’‘’
>>> 
/home/michael/PythonWorks/python/os/par_file.txt
('', '/home/michael/PythonWorks/python/os/par_file.txt')
‘’‘
(5)返回filename,extension元组

os.path.splittext()

*输入文件名,不演示了

(6)返回文件信息

*可以用os.stat

或:

loc = os.path.join(os.pardir,'par_file.txt')
print loc
print os.path.getctime(loc)
'''
../par_file.txt
1420952708.73
'''
'''其他的用法类似:
getatime()
getctime()
getmtime()
getsize()
'''
(6)查询

exists()  #输入文件路径或文件夹路径均可

isabs()   #若输入的是路径+文件名,返回false

isdir()   

isfile()

islink()   #指定路径是否存在且为一个符号链接???

ismount() #是否存在且为一个挂载点

samefile()#是否为同一个文件

均返回 True/False






















(

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值