Python文件的操作2

来源:互联网 发布:2016交通事故数据 编辑:程序博客网 时间:2024/05/23 19:47
#文件的复制、剪切:import shutilshutil.copyfile('hello word.txt', 'hello1.txt')shutil.move('hello1.txt', '..\hello.txt')#move#文件的重命名:import osls = os.listdir('.\\')print '========================'for file in ls:    if 'hello word.txt' == file:        print file        os.rename(file, 'hello2.txt')#delete文件的删除import os.pathif os.path.exists('hello.txt') or os.path.exists('hello2.txt'):    os.remove('hello.txt')    os.remove('hello2.txt')    print 'del'#ini配置文件的读取,写入import ConfigParserconfig = ConfigParser.ConfigParser()config.read('db100.ini')print config.sections()print config.options('config')print config.get('config', 'version')print config.set('config', 'version', '2.3.3')fp = open('db100.ini','w')config.write(fp)fp.close()   #目录的创建和删除   import osos.mkdir('11') os.makedirs('1/2/3')os.rmdir('11')os.removedirs('1/2/3')

0 0