Introduction to reading and writing articles in Python File Management

Source: Internet
Author: User

In the process of computer application, Python file management is easier than other computer languages. If you want to know how Python File Management applies certain modules to file operations, you can watch our articles and hope you can get relevant knowledge from them.

Introduction

The games you 've played use files to save archives; the orders you place are stored in files; obviously, the reports you write in the morning are also saved in files.

File Management is an important part of almost all applications written in any language. Python files are no exception. In this article, we will explore how to use some modules to operate files. We will complete the operations of reading, writing, and adding the file content. There are also some alternative usage.

Read/write files

Of course, the most basic file operation is to read and write data in the file. This is easy to grasp. Open a file to perform the write operation:

 
 
  1. view plaincopy to clipboardprint?  
  2. fileHandle = open ( 'test.txt', 'w' )   

FileHandle = open ('test.txt ', 'w') 'W' indicates that the file will be written into data, and other parts of the statement are well understood. The next step is to write data to a file:

 
 
  1. view plaincopy to clipboardprint?  
  2. fileHandle.write ( 'This is a test.\nReally, it is.' )   
  3. fileHandle.write ( 'This is a test.\nReally, it is.' )  

This statement writes "This is a test." to the first line of the file, "Really, it is." to the second line of the file. Finally, we need to clean up and disable Python file management:

 
 
  1. view plaincopy to clipboardprint?  
  2. fileHandle.close()   

FileHandle. close () as you can see, in Python's object-oriented mechanism, this is indeed very simple. It should be noted that when you use the "w" method to write data in the file again, all the original content will be deleted. If you want to retain the original content, you can use the "a" method to append data to the end of the file:

 
 
  1. view plaincopy to clipboardprint?  
  2. fileHandle = open ( 'test.txt', 'a' )   
  3. fileHandle.write ( '\n\nBottom line.' )   
  4. fileHandle.close()   
  5. fileHandle = open ( 'test.txt', 'a' )  
  6. fileHandle.write ( '\n\nBottom line.' )  
  7. fileHandle.close()  

Read/write files

 
 
  1. view plaincopy to clipboardprint?  
  2. fileHandle = open ( 'test.txt' )   
  3. print fileHandle.read()   
  4. fileHandle.close()   
  5. fileHandle = open ( 'test.txt' )  
  6. print fileHandle.read()  
  7. fileHandle.close()  

The preceding statement reads the entire file and displays its data. We can also read a row in Python file management:

 
 
  1. view plaincopy to clipboardprint?  
  2. fileHandle = open ( 'test.txt' )   
  3. print fileHandle.rehttp://new.51cto.com/wuyou/adline() 
  4. # "This is a test."   
  5. fileHandle.close()   
  6. fileHandle = open ( 'test.txt' )  
  7. print fileHandle.readline() # "This is a test."  
  8. fileHandle.close()   

The above is an introduction to Python file management, and I hope you will find some gains.

Related Article

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.