Analysis of simple Python File Operations

Source: Internet
Author: User

In the Python programming language, there are many useful operation skills worth mastering in learning and practice to help us use this language to implement various functions. Here we will first take a look at the Python file operations related application skills.

Now we will take txt as an example to briefly introduce Python file operations.

The first step is to establish an association... assume that the following file e: test.txt exists.

 
 
  1. This is line #1  
  2. This is line #2  
  3. This is line #3  
  4. END  
  5. >>> f = file('e:\test.txt', 'r') 

The first part of the keyword is the file path and name. Note that \ is required for the path \

The second part is the file mode or permission, which generally has the following three types: "r" (read), "w" (write) and "a" (append ).

Then, you can use

 
 
  1. f_content = infile.read()  
  2. f_content = infile.readlines() 

To read the file content.

 
 
  1. >>> f = file('e:\test.txt', 'r')  
  2. >>> ff_content = f.read()  
  3. >>> print f_content  
  4. This is line #1  
  5. This is line #2  
  6. This is line #3  
  7. END  
  8. >>> f.close()  
  9. >>> 
  10. >>> infile = file('e:\test.txt', 'r')  
  11. >>> f = file('e:\test.txt', 'r')  
  12. >>> for f_line in f.readlines():  
  13. print 'Line:', f_line  
  14. Line: This is line #1  
  15. Line: This is line #2  
  16. Line: This is line #3  
  17. Line: END  
  18. >>> f.close()  
  19. >>> 

Then write the file

 
 
  1. >>> f=file('e:\test.txt','w')  
  2. >>> f.write('billrice')  
  3. >>> f.write('testtest')  
  4. >>> f.write('entern')  
  5. >>> f.writelines(['billrice','ricerice'])  
  6. >>> f.close()  
  7. >>> 
  8. >>> f=file('e:\test.txt','r')  
  9. >>> content=f.read()  
  10. >>> print content  
  11. billricetesttestenter  
  12. billricericerice  
  13. >>> 

In the pythonfile operation, the website is ...in front of f.close(), the C drive has only an empty test.txt, and f. close () serves as the final storage disk.

Delete an object:

 
 
  1. name='e:1.txt' 
  2. os.remove(name) 

Compressed file:

 
 
  1. Import OS
  2. Import zipfile
  3. Import time
  4. # Compressing Directories
  5. Source_dir = r 'f: web'
  6. # Generate file names by Time
  7. Target_file = time. strftime ('% Y % m % d % H % M % s') + '.zip'
  8. MyZipFile = zipfile. ZipFile (target_file, 'w') # compress all files, including subdirectories
  9. For root, dirs, files in OS. walk (source_dir ):
  10. For vfileName in files:
  11. FileName = OS. path. join (root, vfileName)
  12. MyZipFile. write (fileName, fileName, zipfile. ZIP_DEFLATED)
  13. # Compression completed
  14. MyZipFile. close ()

The above is the Python file operation details.

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.