The Python operations Library does not require additional module installation, and the file Operations Class library is an operation supported by the Python language itself.
Determine if file exists: Os.path.isfile (FilePath)
ImportOSImportSYSif __name__=='__main__': FilePath='D:\\ftp\\huawei\\1.txt' ifOs.path.isfile (filePath):#Os.remove () would remove a file. #Os.rmdir () would remove an empty directory. #Shutil.rmtree () would delete a directory and all its contents.Os.remove (FilePath)Print 'file has Exists,was removed ...' #If the file is not exists'll be created.Fileobj=open (FilePath,'W') #Loop the list of dir forFolderinchOs.listdir ('d:\\ftp\\huawei\\20160513'): Fileobj.write (Folder+',') #if forget to close the file oject,the operate are not flush util the current process exit.fileobj.close ()#Read FileFilereadobj=open ('D:\\ftp\\huawei\\2.txt','R') Filewriterobj=open ('D:\\ftp\\huawei\\3.txt','a') Filewriterobj.write ('---------------------------------------------------------\ r \ n') forLineinchfilereadobj.readlines (): Filewriterobj.write ('Select'+ line+'ID Union All') Filereadobj.close () filewriterobj.close ()
Resources:
Https://docs.python.org/3/library/os.html#os.remove
Http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python
Python: Manipulating Files