python 核心编程第九章文件

#encoding=utf-8
from __future__ import unicode_literals #运行时中文编码正确
import os
#fp=open('filename','r','缓冲区大小')
'''------------------------'''
#从文件读取行
#file.readlines()读取所有数据,返回字符串的列表 最常用

#文件迭代,file.next(),每次只读取和显示一行
#for eachLine in f

'''-------------正常读取文件------------'''
filename = raw_input('enter file name:')
f = open(filename,'r')
allLines = f.readlines()
f.close()
#读完所有的行才开始向屏幕输出数据,不如迭代器
for eachLine in allLines:
    print eachLine,
'''----------迭代读取文件---------------'''
filename = raw_input('enter file name:')
f = open(filename,'r')
for eachLine in f:
    print eachLine,
f.close()

'''--------------------输出到文件-------------'''

filename = raw_input('enter file name:')
fobj = open(filename,'w')
#会覆盖原文件
while True:
    aLine = raw_input("enter a line('.' to quit):")
    #从用户接收一行输入,将文本保存到文件中,
    if aLine != '.':    #用户输入‘。’结束输入
        fobj.write('%s%s' % (aLine,os.linesep))
        #raw_input不保留用户输入的换行符,
        #调用write方法时必须加上换行符
        #os.linesep字符串给出当前平台使用的行终止符
        #加上import os
    else:
        break
fobj.close()

'''-----------------seek() tell()---------------'''
# file.seek(off, whence=0) 在文件中移动文件指针, 从 whence ( 0 代表文件其始, 1 代
# 表当前位置, 2 代表文件末尾)偏移 off 字节
# file.tell() 返回当前在文件中的位置
f = open('1','w+')
print f.tell()    #0

f.write('wzy \n')
print f.tell()    #6

f.write('love \n')
print f.tell()    #13

f.seek(-7,1)    #向前移7个字节
print f.tell()    #6
print f.readline()

f.seek(0,0)    #回到最初
print f.tell()
print f.readline()

print f.tell()    #6    回到第二行
print f.readline()

print f.tell()    #13    回到文件末尾
f.close()


'''-----------------------------------'''
#os内建函数 os.path内建函数
# 创建一个文本文件, 写入少量数据,
# 然后重命名, 输出文件内容. 同时还进行了一些辅助性的文件操作,
# 比如遍历目录树和文件路径名处理.
for tmpdir in('/tmp',r'F:\\python'):
    if os.path.isdir(tmpdir):
        break
    else:
        print 'no temp dir available'
        tmpdir = ''

if tmpdir:
    os.chdir(tmpdir)    
    #chdir()/fchdir()a 改变当前工作目录/通过一个文件描述符改变当前工作目录
    cwd = os.getcwd()
    #getcwd()/getcwdu()a返回当前工作目录/功能
    print '***current temp dir'
    print cwd

'''----在临时目录里创建一个子目录-------'''
print '***creating example dir'
os.mkdir('./example2')
# mkdir()/makedirs() 创建目录/创建多层目录
# rmdir()/removedirs() 删除目录/删除多层目录
os.chdir('example')
cwd = os.getcwd()
print '***new working dir:'
print cwd

print '***original dir listing'
print os.listdir(cwd)
#listdir() 列出指定目录的文件

'''-------------创建test文件-------------------'''
print '***creating test file..'
fobj = open('test','w')
fobj.write('foo\n')
fobj.close()
print '***updated dir listing'
print os.listdir(cwd)

print '***renaming test to filetest.txt'
os.rename('test','filetest.txt')
print '***updated dir listing'
print os.listdir(cwd)

path = os.path.join(cwd,os.listdir(cwd)[0])
#join() 将分离的各部分组合成一个路径名
print '***full file pathname'
print path
print '***(pathname,basename)=='
print os.path.split(path)
# split() 返回 (dirname(), basename()) 元组
# splitdrive() 返回 (drivename, pathname) 元组
# splitext() 返回 (filename, extension) 元组
print '***(filename,extension) == '
print os.path.splitext(os.path.basename(path))
#basename() 去掉目录路径, 返回文件名

print '*** displaying file contents:'
fobj = open(path)
for eachLine in fobj:
    print eachLine,
fobj.close()

'''------------删除创建的目录和文件-----------------'''
print '*** deleting test file'
os.remove(path)
print '*** updated directory listing:'
print os.listdir(cwd)
os.chdir(os.pardir)
print '*** deleting test directory'
os.rmdir('example')
print '*** DONE'

 

posted @ 2016-08-11 15:28  zdmlcmepl  阅读(241)  评论(0编辑  收藏  举报