Python: Manipulating Files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.