python文件与目录操作

做图像识别、检索避免不了大量图像样本操作,mark下常用的基本文件与目录操作,对于样本属性标记等至关重要

======================基本文件操作=====================

1. 文件打开

    fp = open('filename', 'pattern')

    打开模式有:

    r       以读的方式打开
   w       以写方式打开,文件首先被清空
   a       以追加模式打开 (从 EOF 开始, 必要时创建新文件)
   r+      以读写模式打开
  w+     以读写模式打开 (参见 w )
  a+      以读写模式打开 (参见 a )
  rb       以二进制读模式打开
  wb     以二进制写模式打开 (参见 w )
  ab      以二进制追加模式打开 (参见 a )
  rb+     以二进制读写模式打开 (参见 r+ )
  wb+   以二进制读写模式打开 (参见 w+ )
  ab+    以二进制读写模式打开 (参见 a+ )

2. 文件读取

    提供三种方法:

  • fp.read():每次读取整个文件,通常用于内容复制
  • fp.readline():每次读取一行,换行符也一并读进,处理时需要特别注意
  • fp.readlines():一次读取整个文件,并存放成一个行的列表,可用for line in readlines()结构进行操作

3. 写文件

  • fp.write(str):将str忠实的写入文件中,不会加换行符
  • fp.writelines(strseq):多行一次性写入,也是忠实于strseq内容,不会添加任何字符

4. 关闭文件

    fp.close()

 

 

========================目录操作=========================

主要是os和os.path模块

1. 目录的创建如删除

    os.mkdir('dir')          --------创建目录

    os.remove('file')      --------删除文件

    os.rmdir('dir')          --------删除空目录

    os.rename(old, new)   ------重命名

2. 目录遍历

     os.listdir('dir')         --------列出dir下的所有子目录和文件

     os.walk(‘dir’)          --------返回dir下的所有root,dirs,files列表

     os.path.walk(‘dir’, callFunc, arg)   --------含有回调函数的遍历

3. 目录获取与判断

    os.getcwd()               --------获得当前工作目录

    os.curdir()                  ---------返回但前目录('.')

    os.path.isdir('dir')     ---------判断dir是否为目录

    os.path.isfile(‘file’)   ---------判断file是否为文件

    os.path.exists('file')  ---------判断是否存在文件或目录file

    os.path.abspath('file')        ---------获得绝对路径

    os.path.basename('file')  --------返回文件名

    os.path.dirname('file')      --------返回文件路径

    os.path.join(path,name) --------连接目录与文件名

4.扩展名分割
   os.path.split(‘file’)         --------分割文件名与目录名

   os.path.splitext(‘file’)    --------分离文件名与扩展名

 

==========================csv文件操作=========================

主要是CSV模块,包括reader, writer, DictReader, DictWriter. register_dialect

1. 获取csv file对象

     csvfile = file('my.csv', 'rb'), 'b'表示是文件对象,也支持list对象

2. csv文件读取

     reader = csv.reader(csvfile),  默认为excel方式,也就是逗号(,)分隔

     reader 中存放的是csv文件中以行为单位的list列表,列表中的每个元素即是csv文件中每行以逗号(,)分隔的元素

3. csv文件写

    writer = csv.writer(csvfile), 默认为excel方式,也就是逗号(,)分隔

    writer.writerow(strlist), 写入一行

    writer.writerows(strlists)

 

reader和writer都是支持迭代类型的,即list,接下来的DictReader, DictWriter支持的是dict类型, 如:fields = ['Name', 'Sex', 'Age'],python中的dict结构如:data = {'Name' :'Kity', 'Sex':'male', 'Age': '18'}

4. DictReader

     reader = csv.DictReader(csv_file)

5. DictWriter

    writer = csv.DictWriter(csv_file, fields)

    writer.writerow(fields), 第一行的filed域要先写入

    writer.writerows(dictlists), 然后将dict的list写入

6. 关闭

    csvfile.close()

   

 

===============================更高级的glob模块=======================

glob是python自己带的一个文件操作相关模块,用它可以查找符合自己目的的文件,就类似于Windows下的文件搜索,支持通配符操作,*,?,[]这三个通配符,主要有glob()和iglob()函数:

1.  glob.glob(pathname)

     Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification. pathname can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like http://www.cnblogs.com/Tools/*/*.gif), and can contain shell-style wildcards. Broken symlinks are included in the results (as in the shell).

2.  glob.iglob(pathname)
     Return an iterator which yields the same values as glob() without actually storing them all simultaneously.

    For example, consider a directory containing only the following files: 1.gif, 2.txt, and card.gif. glob() will produce the following results. Notice how any leading components of the path are preserved.

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值