复习文件操作

print "当前路径:"
        print os.getcwd()
        print "判断是否是一个文件:"
        print os.path.isfile(os.getcwd())
        print "是否是一个目录:"
        print os.path.isdir(os.getcwd())
        print os.system("ls")
        print "判断是否是绝对路径:"
        print os.path.isabs("excp01.py")
        print "检验给出的路径是否真地存:"
        print os.path.exists("/Users/zhouhaijun")
        print "返回一个路径的目录名和文件名"
        print os.path.split("/Users/zhouhaijun")
        print "分离文件名与扩展名"
        print os.path.splitext("tmp1.dat")
        print "找出某个目录下所有的文件,并在每个文件中写入“osTest”"
        for root,dis,files in os.walk("/Users/zhouhaijun/Desktop/hhhhh/osTest"):
            for name in files:
                print name,root,os.path.join(root,name)
                with open(os.path.join(root,name),"w") as fp:
                    fp.write("good! liwen \n 学习walk"
                             "命令")
        print "ok!"

        print "如果某个目录下文件名包含dat后缀名,则把文件后面追加写一行“被我找到了!"
        for root,dirs,files in os.walk("/Users/zhouhaijun/Desktop/hhhhh/osTest"):
            for name in files:
                suffix = os.path.splitext(os.path.join(root,name))[-1]
                if suffix in "dat":
                    print name
                    with open(os.path.join(root,name),"a") as fp:
                        fp.write("\n我找到了!!!")
        print "修改内容:"
        os.system("cd /Users/zhouhaijun/Desktop/hhhhh/osTest")
        print "获取当前路径"
        os.chdir("/Users/zhouhaijun/Desktop/hhhhh/osTest")
        print "获取当前目录:"
        print os.getcwd()
        for line in fileinput.input("dd",inplace = 1):
            line = line.replace("walk","\n复习fileinput.input命令")
            print line

        print "seek函数:"
        with open("dd","r+") as f:
            old = f.read()
            print "内容是:",old
            f.seek(2)
            f.write("liwen is very googd!!!!")

结果:

当前路径:
/Users/zhouhaijun/PycharmProjects/Test/PythonTest/dome01
判断是否是一个文件:
False
是否是一个目录:
True
Monday.py
init.py
contect_list.txt
excp01.py
one_01.py
phone.dat
test.kpl
tmp1.dat
tmp2.dat
0
判断是否是绝对路径:
False
检验给出的路径是否真地存:
True
返回一个路径的目录名和文件名
('/Users', 'zhouhaijun')
分离文件名与扩展名
('tmp1', '.dat')
找出某个目录下所有的文件,并在每个文件中写入“osTest”
dd /Users/zhouhaijun/Desktop/hhhhh/osTest /Users/zhouhaijun/Desktop/hhhhh/osTest/dd
liwen.dat /Users/zhouhaijun/Desktop/hhhhh/osTest /Users/zhouhaijun/Desktop/hhhhh/osTest/liwen.dat
ok!
如果某个目录下文件名包含dat后缀名,则把文件后面追加写一行“被我找到了!
dd
修改内容:
获取当前路径
获取当前目录:
/Users/zhouhaijun/Desktop/hhhhh/osTest
seek函数:
内容是: good! liwen

学习
复习fileinput.input命令命令

我找到了!!!