• python 文件操作


    1、基本文件格式

    import sys
    
    def start():
        print(__file__)
        
    if __name__ == "__main__":
        argvList = sys.argv
        start()

    2、文件操作

    import os #文件操作引用
    #创建或覆盖文件
    with open(os.path.dirname(__file__)+'\1.txt',"w") as fw:            
        fw.write(f.read())    
    #读取文件
    with open(os.path.dirname(__file__)+'\files\1.txt') as f:
        print(f.read())
    #获取文件夹列表,并循环输出
    for path in os.listdir(os.path.dirname(__file__)+'\files'):
            print(path);
    # -*- coding:utf-8 -*-
    import sys
    import os
    
    def start():
        #读取配置文件
        print(__file__) 
        print(os.listdir(os.path.dirname(__file__)+'\files'));
        with open(os.path.dirname(__file__)+'\1.txt',"w") as fw:    
            for path in os.listdir(os.path.dirname(__file__)+'\files'):
                with open(os.path.dirname(__file__)+'\files\'+ path) as f:  
                    fw.write(f.read())
                    fw.write('
    ')
                    
        #os.system('pause')
                               
        
    if __name__ == "__main__":
        argvList = sys.argv
        start()
    案例-合并文件
  • 相关阅读:
    vagrant
    webapp开发
    UIViewAnimationOptions swift 2
    swift 2
    autolayout 总结
    apache 403错
    hadoop配置优化
    hadoop配置优化
    使用spark访问elasticsearch的数据
    使用spark访问elasticsearch的数据
  • 原文地址:https://www.cnblogs.com/tanl/p/13789040.html
Copyright © 2020-2023  润新知