python笔记(十) - 异常和文件处理

本文介绍了Python中文件操作及异常处理的方法,并演示了如何利用OS模块进行路径处理、文件检索等常见任务。

 1.打开一个不存在的文件:

try:
      f 
= open('demon.txt','w')
      
#print s
except IOError:
      
print 'file is not exits!'
except:
      
print 'other exception'
else:
      
print 'file exits!' 

 

用trt...exceot不捕获异常。如果没有异常发生,则执行else后面的内容

和C#一样,python捕获异常也是按照顺序的,上例中,先捕获IOError异常,如果没有写明异常类型则捕获所有的异常,你去可去掉第三行的#会发生什么。


2.上例中,我们使用open打开了一个文件,这个方法可以接受三个参数,文件名,模式和缓冲区,打开文件的模式有三种:‘r’-只读模式,也是默认模式,'w'-写模式,'a'-追加模式

在写模式和追加模式两种情况下,如果文件不存在会自动创建文件,读模式则会抛出异常

>>> f = open('demon.txt','w')
>>> f.write('line1')
>>> f.close()
>>> file('demon.txt').read()
'line1' 

 


3.接下来看看OS模块

>>> import os
>>> os.path.join("c:\\music\\ap\\""mahadeva.mp3")  
'c:\\music\\ap\\mahadeva.mp3'
>>> os.path.join("c:\\music\\ap""mahadeva.mp3")   
'c:\\music\\ap\\mahadeva.mp3'
>>> os.path.expanduser("~")                         
'c:\\Documents and Settings\\mpilgrim\\My Documents'
>>> os.path.join(os.path.expanduser("~"), "Python"
'c:\\Documents and Settings\\mpilgrim\\My Documents\\Python'

 expanduser 将对使用 ~ 来表示当前用户根目录的路径名

4.分割路径

>>> os.path.split("c:\\music\\ap\\mahadeva.mp3")                        
(
'c:\\music\\ap''mahadeva.mp3')
>>> (filepath, filename) = os.path.split("c:\\music\\ap\\mahadeva.mp3"
>>> filepath                                                            
'c:\\music\\ap'
>>> filename                                                            
'mahadeva.mp3'
>>> (shortname, extension) = os.path.splitext(filename)                 
>>> shortname
'mahadeva'
>>> extension
'.mp3'

 5.列出所有文件

>>> os.listdir("c:\\music\\_singles\\")              
[
'a_time_long_forgotten_con.mp3''hellraiser.mp3',
'kairo.mp3''long_way_home1.mp3''sidewinder.mp3'
'spinning.mp3']
>>> dirname = "c:\\"
>>> os.listdir(dirname)                              
[
'AUTOEXEC.BAT''boot.ini''CONFIG.SYS''cygwin',
'docbook''Documents and Settings''Incoming''Inetpub''IO.SYS',
'MSDOS.SYS''Music''NTDETECT.COM''ntldr''pagefile.sys',
'Program Files''Python20''RECYCLER',
'System Volume Information''TEMP''WINNT']
>>> [f for f in os.listdir(dirname)
if os.path.isfile(os.path.join(dirname, f))] 
[
'AUTOEXEC.BAT''boot.ini''CONFIG.SYS''IO.SYS''MSDOS.SYS',
'NTDETECT.COM''ntldr''pagefile.sys']
>>> [f for f in os.listdir(dirname)
 
if os.path.isdir(os.path.join(dirname, f))]  
[
'cygwin''docbook''Documents and Settings''Incoming',
'Inetpub''Music''Program Files''Python20''RECYCLER',
'System Volume Information''TEMP''WINNT']

 listdir 函数接收一个路径名,它返回那个目录的内容的一个 list

6.

>>> dirname = 'c:\\'
>>> [os.path.normcase(f) for f in os.listdir(dirname)]
[
'adclog''autoexec.bat''boot''boot.bak''boot.ini''bootfont.bin''config.msi''config.sys''cygwin''documents and settings''download''downloads''ftc2008''grldr''img''inetpub''io.sys''krecycle''menu.lst''msdos.sys''msocache''ntdetect.com''ntldr''pagefile.sys''program files''python26''recycler''system volume information''windows']
>>> [os.path.join(dirname,f) for f in os.listdir(dirname) if os.path.splitext(f)[1in ['.ini']]
[
'c:\\boot.ini']
>>> 

 我们使用 os.path.normcase(f) 根据操作系统的缺省值对大小写进行标准化处理

7.一种更强大的方法

>>> import glob
>>> glob.glob('c:\\*.ini')
[
'c:\\boot.ini']
>>> 

 它查找出在C目录下所有的.ini文件 

 

转载于:https://www.cnblogs.com/uwebs/archive/2009/04/27/1444631.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值