python OS模块

来源:互联网 发布:2016淘宝联盟提现 编辑:程序博客网 时间:2024/06/03 21:28

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


1、文件夹操作



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


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

import osos.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()   #不要忘了getcwdfiles = os.listdir(cwd)print files# >>>['try_dir_rename', 'try_dir', 'try_os.py']

(5)生成目录下所有文件名的所有完整路径(os.walk)

import oscwd = 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 oscwd = 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_renamef = 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 locfile_name = os.path.basename(loc)print file_name'''>>>/home/michael/PythonWorks/python/os/home/michael/PythonWorks/python/os/par_file.txtpar_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 locprint os.path.getctime(loc)'''../par_file.txt1420952708.73''''''其他的用法类似:getatime()getctime()getmtime()getsize()'''

(6)查询

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

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

isdir()   

isfile()

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

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

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

均返回 True/False






















(

0 0
原创粉丝点击