码迷,mamicode.com
首页 > 编程语言 > 详细

Python:操作文件

时间:2016-07-31 00:10:38      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

python操作文件库不需要安装其他module,文件操作类库是python语言自身支持的操作。

判定文件是否存在:os.path.isfile(filePath)

 

import os
import sys

if __name__==__main__:
    filePath=d:\\FTP\\HUAWEI\\1.txt
    
    if os.path.isfile(filePath):
        #os.remove() will remove a file.
        #os.rmdir() will remove an empty directory.
        #shutil.rmtree() will delete a directory and all its contents.
        os.remove(filePath)
        print file has exists,was removed...
        
    #if the file not exists will be created.
    fileObj=open(filePath,w)

    # loop the list of dir
    for folder in os.listdir(D:\\FTP\\HUAWEI\\20160513):
        fileObj.write(folder+,)

    #if forget to close the file oject,the operate is not flush util the current process exit.
    fileObj.close()

    #read file
    fileReadObj=open(d:\\FTP\\HUAWEI\\2.txt,r)
    fileWriterObj=open(d:\\FTP\\HUAWEI\\3.txt,a)
    fileWriterObj.write(---------------------------------------------------------\r\n)
    for line in fileReadObj.readlines():
        fileWriterObj.write(select + line+ id union all )
    fileReadObj.close()
    fileWriterObj.close()

参考资料:

https://docs.python.org/3/library/os.html#os.remove

http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python

Python:操作文件

标签:

原文地址:http://www.cnblogs.com/yy3b2007com/p/5721885.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!